作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
official guide不完整,others用于 CI2。
所以我给你一个我自己检查过的教程。
我知道所以鼓励用户 to answer their own questions .
最佳答案
安装教义
(以下指令修改自:Doctrine 2 ORM’s documentation - Installation and Configuration)
可以使用 Composer 安装教义:
C:\>composer install doctrine/orm
{
"require": {
"doctrine/orm": "*"
}
}
composer install
从你的命令行。 vendor
有许多子文件夹和数百个 php 文件。移动这个
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
Doctrine.php
(见下文)你会:
require_once FCPATH . 'vendor/autoload.php'; // FCPATH is a CI constant specifies the path to the front controller.
vendor
中包含的所有文件夹和文件。其他地方,比如
third_party
并相应地调整您的 Doctrine.php。
system/application/libraries
, 创建一个名为 Doctrine.php
的文件并将以下代码复制/粘贴到文件中。这将成为 Doctrine2 实体管理器的包装器/ Bootstrap 。<?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';
// load Doctrine
require_once FCPATH . 'vendor/autoload.php';
// or, if you installed another way, you could:
// require_once APPPATH.'third_party/Doctrine/Common/ClassLoader.php';
// load the Doctrine classes
$doctrineClassLoader = new ClassLoader('Doctrine', APPPATH.'libraries');
// or, if installed in third_party:
// $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;
endif;
$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 (recommended to remove for production)
$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);
}
}
application/config/autoload.php
中的数组来自动加载您的 Doctrine 库。文件:$this->load->library('doctrine');
applications/third_party
中安装了 Doctrine.php ,你会使用:
$autoload[‘libraries’] = array('third_party/doctrine');
$this->load->library('third_party/doctrine');
$symfonyClassLoader = new ClassLoader('Symfony', APPPATH.'third_party/Doctrine');
$symfonyClassLoader->register();
<?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
php cli-doctrine.php orm:convert-mapping --from-database annotation models/Entities
sudo apt-get install php-apc
sudo /etc/init.d/apache2 restart
EchoSqlLogger
- 关闭
autoGenerateProxyClasses
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;
}
关于codeigniter - 如何在 CodeIgniter 3 中安装 Doctrine,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33209930/
我是一名优秀的程序员,十分优秀!