如何在CodeIgniter 3中安装条令
未完成,并且是针对CI2的 所以我给你们一个我自己检查过的教程,它正在运行如何在CodeIgniter 3中安装条令,codeigniter,doctrine-orm,codeigniter-3,Codeigniter,Doctrine Orm,Codeigniter 3,未完成,并且是针对CI2的 所以我给你们一个我自己检查过的教程,它正在运行 我知道这是鼓励用户的。安装原则 (以下说明修改自:) 条令可以安装在以下设备上: 从命令行(例如Windows:Start>cmd)更改到应安装文件的文件夹(例如htdocs/my_项目) 使用Composer安装文件: a。运行C:\>composer安装/orm 或: b。在composer.json文件中定义以下要求: { "require": { "doctrine/orm": "*" } } 然后
我知道这是鼓励用户的。安装原则 (以下说明修改自:) 条令可以安装在以下设备上:
C:\>composer安装/orm
或:
b。在composer.json文件中定义以下要求:
{
"require": {
"doctrine/orm": "*"
}
}
然后从命令行调用composer install
vendor
。将此供应商
文件夹移动到CodeIgniter应用程序树中。为简单起见,您可以将其放在此处:
/application
/config
/controllers
/libraries
Doctrine.php <-- the doctrine bootstrap/wrapper file
/third_party
/vendor <-- the folder installed by Composer. don't touch the files or folders below it -- install all together as one happy family.
/bin
/composer
/doctrine
/symfony
autoload.php <-- Doctrine.php opens this to load its files
您还可以将vendor
中包含的所有文件夹和文件安装到其他地方,比如third\u party
中,并相应地调整doctor.php
与CodeIgniter集成 (以下说明修改自:)
系统/应用程序/库
中,创建一个名为条令.php
的文件,并将以下代码复制/粘贴到该文件中。这将是Doctrine2实体管理器的包装器/引导程序
您的doctor.php库文件应如下所示(您可以根据需要对其进行自定义):
您需要将应用程序EntityManager注册到console工具,以便通过在应用程序目录中创建包含以下内容的cli-doctor.php文件来使用这些任务:
<?php
/**
* Doctrine CLI bootstrap for CodeIgniter
*
*/
define('APPPATH', dirname(__FILE__) . '/');
define('BASEPATH', APPPATH . '/../system/');
define('ENVIRONMENT', 'development');
require APPPATH.'libraries/Doctrine.php';
$doctrine = new Doctrine;
$em = $doctrine->em;
$helperSet = new \Symfony\Component\Console\Helper\HelperSet(array(
'db' => new \Doctrine\DBAL\Tools\Console\Helper\ConnectionHelper($em->getConnection()),
'em' => new \Doctrine\ORM\Tools\Console\Helper\EntityManagerHelper($em)
));
\Doctrine\ORM\Tools\Console\ConsoleRunner::run($helperSet);
?>
从数据库生成映射类:
php cli-doctrine.php orm:convert-mapping --from-database annotation models/Entities
如果出现此错误:
致命错误:调用未定义的函数doctor\Common\Cache\apc\u fetch()
为PHP安装APC扩展:
sudo apt-get install php-apc
sudo /etc/init.d/apache2 restart
对于生产模式: Doctrine建议更改Doctrine.php中的以下设置: -使用真正的缓存系统,如APC -禁用EchoSqlLogger
-关闭AutogenerateProxy类
下一步是什么 要在CI中使用条令,请从控制器调用它,例如: 应用程序/controllers/my_controller.php:
function doctrine_orm()
{
$this->load->library('Doctrine');
$em = $this->doctrine->em;
// do Doctrine stuff
$productRepository = $em->getRepository('Product');
$products = $productRepository->findAll();
foreach ($products as $product):
echo sprintf("-%s\n", $product->getName());
endforeach;
}
然而,在执行任何条令之前,必须首先将数据库表映射到条令“实体”。在这里学习方法:对于CI3+HMVC+原则2.4
<?php
use Doctrine\Common\Annotations\AnnotationReader;
use Doctrine\Common\Cache\ArrayCache;
use Doctrine\Common\ClassLoader;
use Doctrine\Common\EventManager;
use Doctrine\DBAL\Event\Listeners\MysqlSessionInit;
use Doctrine\DBAL\Logging\EchoSQLLogger;
use Doctrine\ORM\Configuration;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Mapping\Driver\AnnotationDriver;
use Doctrine\ORM\Tools\SchemaTool;
use Gedmo\Sluggable\SluggableListener;
use Gedmo\Timestampable\TimestampableListener;
use Gedmo\Tree\TreeListener;
class Doctrine
{
public $em = null;
public $tool = null;
public function __construct()
{
// Is the config file in the environment folder?
if (!defined('ENVIRONMENT') OR !file_exists($file_path = APPPATH . 'config/' . ENVIRONMENT . '/database.php')) {
$file_path = APPPATH . 'config/database.php';
}
// load database configuration from CodeIgniter
require $file_path;
// Set up class loading. You could use different autoloaders, provided by your favorite framework,
// if you want to.
require_once APPPATH . 'vendor/doctrine/common/lib/Doctrine/Common/ClassLoader.php';
$doctrineClassLoader = new ClassLoader('Doctrine', APPPATH . 'libraries');
$doctrineClassLoader->register();
$entitiesClassLoader = new ClassLoader('models', rtrim(APPPATH, "/"));
$entitiesClassLoader->register();
$proxiesClassLoader = new ClassLoader('Proxies', APPPATH . 'proxies');
$proxiesClassLoader->register();
foreach (glob(APPPATH . 'modules/*', GLOB_ONLYDIR) as $m) {
$module = str_replace(APPPATH . 'modules/', '', $m);
$loader = new ClassLoader($module, APPPATH . 'modules');
$loader->register();
}
$evm = new EventManager;
// timestampable
$evm->addEventSubscriber(new TimestampableListener);
// sluggable
$evm->addEventSubscriber(new SluggableListener);
// tree
$evm->addEventSubscriber(new TreeListener);
// Set up caches
$config = new Configuration;
$cache = new ArrayCache;
$config->setMetadataCacheImpl($cache);
$driverImpl = $config->newDefaultAnnotationDriver(array(APPPATH . 'models/Entities'));
$config->setMetadataDriverImpl($driverImpl);
$config->setQueryCacheImpl($cache);
$config->setQueryCacheImpl($cache);
// Proxy configuration
$config->setProxyDir(APPPATH . '/proxies'); //must be set to 777
$config->setProxyNamespace('Proxies');
// Set up logger
$logger = new EchoSQLLogger;
$config->setSQLLogger($logger);
if (ENVIRONMENT == "development") {
$config->setAutoGenerateProxyClasses(true);
} else {
$config->setAutoGenerateProxyClasses(false);
}
// Database connection information
$connectionOptions = array(
'driver' => 'pdo_mysql',
'user' => $db[$active_group]['username'],
'password' => $db[$active_group]['password'],
'host' => $db[$active_group]['hostname'],
'dbname' => $db[$active_group]['database']
);
// Create EntityManager
$this->em = EntityManager::create($connectionOptions, $config);
// Force UTF-8
$this->em->getEventManager()->addEventSubscriber(new MysqlSessionInit('utf8', 'utf8_unicode_ci'));
// Schema Tool
$this->tool = new SchemaTool($this->em);
}
}
遵循这些步骤,不要工作。。。条令2.4和代码点火器3.1.4。
php cli-doctrine.php orm:convert-mapping --from-database annotation models/Entities
sudo apt-get install php-apc
sudo /etc/init.d/apache2 restart
function doctrine_orm()
{
$this->load->library('Doctrine');
$em = $this->doctrine->em;
// do Doctrine stuff
$productRepository = $em->getRepository('Product');
$products = $productRepository->findAll();
foreach ($products as $product):
echo sprintf("-%s\n", $product->getName());
endforeach;
}
<?php
use Doctrine\Common\Annotations\AnnotationReader;
use Doctrine\Common\Cache\ArrayCache;
use Doctrine\Common\ClassLoader;
use Doctrine\Common\EventManager;
use Doctrine\DBAL\Event\Listeners\MysqlSessionInit;
use Doctrine\DBAL\Logging\EchoSQLLogger;
use Doctrine\ORM\Configuration;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Mapping\Driver\AnnotationDriver;
use Doctrine\ORM\Tools\SchemaTool;
use Gedmo\Sluggable\SluggableListener;
use Gedmo\Timestampable\TimestampableListener;
use Gedmo\Tree\TreeListener;
class Doctrine
{
public $em = null;
public $tool = null;
public function __construct()
{
// Is the config file in the environment folder?
if (!defined('ENVIRONMENT') OR !file_exists($file_path = APPPATH . 'config/' . ENVIRONMENT . '/database.php')) {
$file_path = APPPATH . 'config/database.php';
}
// load database configuration from CodeIgniter
require $file_path;
// Set up class loading. You could use different autoloaders, provided by your favorite framework,
// if you want to.
require_once APPPATH . 'vendor/doctrine/common/lib/Doctrine/Common/ClassLoader.php';
$doctrineClassLoader = new ClassLoader('Doctrine', APPPATH . 'libraries');
$doctrineClassLoader->register();
$entitiesClassLoader = new ClassLoader('models', rtrim(APPPATH, "/"));
$entitiesClassLoader->register();
$proxiesClassLoader = new ClassLoader('Proxies', APPPATH . 'proxies');
$proxiesClassLoader->register();
foreach (glob(APPPATH . 'modules/*', GLOB_ONLYDIR) as $m) {
$module = str_replace(APPPATH . 'modules/', '', $m);
$loader = new ClassLoader($module, APPPATH . 'modules');
$loader->register();
}
$evm = new EventManager;
// timestampable
$evm->addEventSubscriber(new TimestampableListener);
// sluggable
$evm->addEventSubscriber(new SluggableListener);
// tree
$evm->addEventSubscriber(new TreeListener);
// Set up caches
$config = new Configuration;
$cache = new ArrayCache;
$config->setMetadataCacheImpl($cache);
$driverImpl = $config->newDefaultAnnotationDriver(array(APPPATH . 'models/Entities'));
$config->setMetadataDriverImpl($driverImpl);
$config->setQueryCacheImpl($cache);
$config->setQueryCacheImpl($cache);
// Proxy configuration
$config->setProxyDir(APPPATH . '/proxies'); //must be set to 777
$config->setProxyNamespace('Proxies');
// Set up logger
$logger = new EchoSQLLogger;
$config->setSQLLogger($logger);
if (ENVIRONMENT == "development") {
$config->setAutoGenerateProxyClasses(true);
} else {
$config->setAutoGenerateProxyClasses(false);
}
// Database connection information
$connectionOptions = array(
'driver' => 'pdo_mysql',
'user' => $db[$active_group]['username'],
'password' => $db[$active_group]['password'],
'host' => $db[$active_group]['hostname'],
'dbname' => $db[$active_group]['database']
);
// Create EntityManager
$this->em = EntityManager::create($connectionOptions, $config);
// Force UTF-8
$this->em->getEventManager()->addEventSubscriber(new MysqlSessionInit('utf8', 'utf8_unicode_ci'));
// Schema Tool
$this->tool = new SchemaTool($this->em);
}
}