official guide未完成,others用於CI2。如何在CodeIgniter中安裝Doctrine 3
所以我給你一個教程我自己檢查工作。
我知道SO鼓勵用戶to answer their own questions。
official guide未完成,others用於CI2。如何在CodeIgniter中安裝Doctrine 3
所以我給你一個教程我自己檢查工作。
我知道SO鼓勵用戶to answer their own questions。
安裝學說
Doctrine 2 ORM’s documentation - Installation and Configuration
學說可以與Composer安裝。在composer.json文件 定義如下要求:
{
"require": {
"doctrine/orm": "*"
}
}
然後調用作曲家從您的命令行安裝。
使用CodeIgniter
Doctrine 2 ORM’s documentation - Integrating with CodeIgniter
這裏集成的步驟是: 添加php文件到您的系統/應用/庫文件夾,名爲Doctrine.php。這將成爲D2實體管理器的包裝器/引導程序。 將Doctrine文件夾(包含Common,DBAL和ORM的文件夾)放入third_party文件夾中。 如果你想,打開你的config/autoload.php文件,並自動加載你的學說庫:$autoload[‘libraries’] = array(‘doctrine’);
創建您的學說笨庫
現在,這裏是你的Doctrine.php文件應該是什麼樣子。根據您的需求定製它。
<?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);
}
}
設置命令行工具
主義附帶了大量的命令行工具,這些工具在開發過程中非常有用。
檢查這些行存在於Doctrine.php文件,加載Symfony的類使用命令行工具(和YAML映射文件):
$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');
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命令行這個腳本,應該看到的命令列表提供給你。
php cli-doctrine.php orm:convert-mapping --from-database annotation models/Entities
,如果你得到這個錯誤: 致命錯誤:調用未定義功能主義\ COMMON \緩存\ apc_fetch() 安裝APC擴展
php cli-doctrine.php
從數據庫生成映射類對於PHP:
sudo apt-get install php-apc
sudo /etc/init.d/apache2 restart
對於生產模式你要使用一個真正的緩存系統象APC,擺脫EchoSqlLogger
,並在Doctrine.php
關閉autoGenerateProxyClasses
對於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);
}
}
按照這些步驟不起作用... Doctrine 2.4和CodeIgniter 3.1.4。 – omixam