如何在CodeIgniter 3中安装Doctrine [英] How to install Doctrine in CodeIgniter 3
问题描述
所以我给你一个我自己检查过的教程。
我知道SO鼓励用户回答自己的问题。
安装教义
: Doctrine可以安装作曲家。
在composer.json文件中定义以下要求:
{
require:{
doctrine / orm:*
}
}
$ b
与CodeIgniter整合
Doctrine 2 ORM的文档 - 与CodeIgniter集成这里是步骤:
将php文件添加到您的system / application / libraries文件夹中,名为Doctrine.php。这将是您的包装器/引导D2实体管理器。
将Doctrine文件夹(包含Common,DBAL和ORM的文件夹)放在third_party文件夹中。
如果你愿意,打开config / autoload.php文件并自动加载你的Doctrine库: $ autoload ['libraries'] = array('doctrine');
创建您的Doctrine CodeIgniter库
现在,这里是您的教义。 php文件应该看起来像。
<?php
/ **
* Doctrine 2.4 bootstrap
*
* /
使用Doctrine\Common\ClassLoader,
Doctrine\ORM\Configuration,
Doctrine\ORM\EntityManager ,
Doctrine\Common\Cache\ArrayCache,
Doctrine\DBAL\Logging\EchoSQLLogger;
class Doctrine {
public $ em = null;
public function __construct()
{
//从CodeIgniter加载数据库配置
require_once APPPATH.'config / database.php';
//包括Doctrine的ClassLoader类
require_once APPPATH.'third_party / Doctrine / Common / ClassLoader.php';
//加载Doctrine类
$ doctrineClassLoader = new ClassLoader('Doctrine',APPPATH.'third_party');
$ doctrineClassLoader-> register();
//加载实体
$ entityClassLoader = new ClassLoader('Entities',APPPATH.'models');
$ entityClassLoader-> register();
//加载代理实体
$ proxiesClassLoader = new ClassLoader('Proxies',APPPATH.'models / proxies');
$ proxiesClassLoader-> register();
//加载Symfony2类
//这是YAML映射文件和命令行界面(cli-doctrine.php)必需的
$ symfonyClassLoader = new ClassLoader('Symfony',APPPATH。 'third_party / Doctrine');
$ symfonyClassLoader-> register();
//设置配置
$ config = new Configuration;
//设置缓存
if(ENVIRONMENT =='development')//在index.php中设置环境
//为开发模式设置简单数组缓存
$ cache = new \Doctrine\Common\Cache\ArrayCache;
else
//使用APC为生产模式设置缓存
$ cache = new \Doctrine\Common\Cache\ApcCache;
$ config-> setMetadataCacheImpl($ cache);
$ config-> setQueryCacheImpl($ cache);
//设置注释驱动程序
$ driver = new \Doctrine\ORM\Mapping\Driver\PHPDriver(APPPATH.'models / Mappings');
$ config-> setMetadataDriverImpl($ driver);
//代理配置
$ config-> setProxyDir(APPPATH。'/ models / Proxies');
$ config-> setProxyNamespace('Proxies');
//设置记录器
$ logger = new EchoSQLLogger;
$ config-> setSQLLogger($ logger);
$ config-> setAutoGenerateProxyClasses(TRUE); //仅用于开发
//数据库连接信息
$ connectionOptions = array(
'driver'=>'pdo_mysql',
'user'=> ; $ db ['default'] ['username'],
'password'=> $ db ['default'] ['password'],
'host'=> $ db [ 'default'] ['hostname'],
'dbname'=> $ db ['default'] ['database']
);
//创建EntityManager,并将其存储在我们的CodeIgniter控制器中使用
$ this-> em = EntityManager :: create($ connectionOptions,$ config);
}
}
设置命令行工具
Doctrine附带了许多在开发过程中非常有用的命令行工具。
检查Doctrine.php文件中是否存在这些行,以使用命令行工具(和YAML映射文件)加载Symfony类:
$ symfonyClassLoader = new ClassLoader('Symfony',APPPATH.'third_party / Doctrine');
$ symfonyClassLoader-> register();
您需要将您的应用程序EntityManager注册到控制台工具以通过创建cli -doctrine.php文件,应用程序目录中包含以下内容:
<?php
/ **
* Doctrine CLI bootstrap for CodeIgniter
*
* /
define('APPPATH',dirname(__ FILE__)。'/');
define('BASEPATH',APPPATH。'/../system/');
define('ENVIRONMENT','development');
需要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命令行运行此脚本,并应该看到可用的命令列表。
php cli-doctrine.php
从数据库生成映射类:
php cli-doctrine.php orm:convert-mapping - 从数据库注释模型/实体
如果得到此错误:
致命错误:调用未定义函数Doctrine\Common\Cache\apc_fetch()
安装PHP的APC扩展:
sudo apt-get install php-apc
sudo /etc/init.d/apache2 restart
对于生产模式,您将需要使用像APC这样的真正的缓存系统,除去 EchoSqlLogger
,并关闭Doctrine.php中的 autoGenerateProxyClasses
The official guide is not complete, and others are for CI2.
So i give you a tutorial i myself checked is working.
I know SO encourage users to answer their own questions.
Install Doctrine
Doctrine 2 ORM’s documentation - Installation and Configuration
Doctrine can be installed with Composer. Define the following requirement in your composer.json file:
{
"require": {
"doctrine/orm": "*"
}
}
Then call composer install from your command line.
Integrating with CodeIgniter
Doctrine 2 ORM’s documentation - Integrating with CodeIgniter
Here are the steps:
Add a php file to your system/application/libraries folder called Doctrine.php. This is going to be your wrapper/bootstrap for the D2 entity manager.
Put the Doctrine folder (the one that contains Common, DBAL, and ORM) inside the third_party folder.
If you want, open your config/autoload.php file and autoload your Doctrine library: $autoload[‘libraries’] = array(‘doctrine’);
Creating your Doctrine CodeIgniter library
Now, here is what your Doctrine.php file should look like. Customize it to your needs.
<?php
/**
* Doctrine 2.4 bootstrap
*
*/
use Doctrine\Common\ClassLoader,
Doctrine\ORM\Configuration,
Doctrine\ORM\EntityManager,
Doctrine\Common\Cache\ArrayCache,
Doctrine\DBAL\Logging\EchoSQLLogger;
class Doctrine {
public $em = null;
public function __construct()
{
// load database configuration from CodeIgniter
require_once APPPATH.'config/database.php';
// include Doctrine's ClassLoader class
require_once APPPATH.'third_party/Doctrine/Common/ClassLoader.php';
// load the Doctrine classes
$doctrineClassLoader = new ClassLoader('Doctrine', APPPATH.'third_party');
$doctrineClassLoader->register();
// load the entities
$entityClassLoader = new ClassLoader('Entities', APPPATH.'models');
$entityClassLoader->register();
// load the proxy entities
$proxiesClassLoader = new ClassLoader('Proxies', APPPATH.'models/proxies');
$proxiesClassLoader->register();
// load Symfony2 classes
// this is necessary for YAML mapping files and for Command Line Interface (cli-doctrine.php)
$symfonyClassLoader = new ClassLoader('Symfony', APPPATH.'third_party/Doctrine');
$symfonyClassLoader->register();
// Set up the configuration
$config = new Configuration;
// Set up caches
if(ENVIRONMENT == 'development') // set environment in index.php
// set up simple array caching for development mode
$cache = new \Doctrine\Common\Cache\ArrayCache;
else
// set up caching with APC for production mode
$cache = new \Doctrine\Common\Cache\ApcCache;
$config->setMetadataCacheImpl($cache);
$config->setQueryCacheImpl($cache);
// set up annotation driver
$driver = new \Doctrine\ORM\Mapping\Driver\PHPDriver(APPPATH.'models/Mappings');
$config->setMetadataDriverImpl($driver);
// Proxy configuration
$config->setProxyDir(APPPATH.'/models/Proxies');
$config->setProxyNamespace('Proxies');
// Set up logger
$logger = new EchoSQLLogger;
$config->setSQLLogger($logger);
$config->setAutoGenerateProxyClasses( TRUE ); // only for development
// Database connection information
$connectionOptions = array(
'driver' => 'pdo_mysql',
'user' => $db['default']['username'],
'password' => $db['default']['password'],
'host' => $db['default']['hostname'],
'dbname' => $db['default']['database']
);
// Create EntityManager, and store it for use in our CodeIgniter controllers
$this->em = EntityManager::create($connectionOptions, $config);
}
}
Setting up the Command Line Tool
Doctrine ships with a number of command line tools that are very helpful during development.
Check if these lines exists in the Doctrine.php file, to load Symfony classes for using the Command line tools (and for YAML mapping files):
$symfonyClassLoader = new ClassLoader('Symfony', APPPATH.'third_party/Doctrine');
$symfonyClassLoader->register();
You need to register your applications EntityManager to the console tool to make use of the tasks by creating a cli-doctrine.php file in the application directory with the following content:
<?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);
?>
Now run this script through the PHP command-line and should see a list of commands available to you.
php cli-doctrine.php
Generate mapping classes from database:
php cli-doctrine.php orm:convert-mapping --from-database annotation models/Entities
if you get this error: Fatal error: Call to undefined function Doctrine\Common\Cache\apc_fetch() install the APC extension for PHP:
sudo apt-get install php-apc
sudo /etc/init.d/apache2 restart
For production mode you'll want to use a real caching system like APC, get rid of EchoSqlLogger
, and turn off autoGenerateProxyClasses
in Doctrine.php
这篇关于如何在CodeIgniter 3中安装Doctrine的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!