Luc*_*ini 9 codeigniter doctrine-orm codeigniter-3
所以我给你一个我自己检查过的教程.
我知道SO鼓励用户回答他们自己的问题.
安装Doctrine
(以下说明经过修改:Doctrine 2 ORM的文档 - 安装和配置)
可以使用Composer安装Doctrine :
从命令行(例如Windows:Start> cmd),切换到应安装文件的文件夹(例如htdocs/my_project).
使用Composer安装文件:
一个.跑C:\>composer install doctrine/orm
要么:
湾 在composer.json文件中定义以下要求:
{
"require": {
"doctrine/orm": "*"
}
}
Run Code Online (Sandbox Code Playgroud)
然后composer install从命令行调用.
Composer将安装一个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
Run Code Online (Sandbox Code Playgroud)
然后在你的库文件中Doctrine.php(见下文)你会:
require_once FCPATH . 'vendor/autoload.php'; // FCPATH is a CI constant specifies the path to the front controller.
Run Code Online (Sandbox Code Playgroud)
您还可以安装vendor其他地方包含的所有文件夹和文件,third_party并相应地调整您的Doctrine.php.
与CodeIgniter集成
(以下说明修改自:Doctrine 2 ORM的文档 - 与CodeIgniter集成)
创建Doctrine库:在您的文件夹中system/application/libraries,创建一个名为的文件Doctrine.php,并将以下代码复制/粘贴到该文件中.这将成为Doctrine2实体管理器的包装器/引导程序.
您的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';
// 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);
}
}
Run Code Online (Sandbox Code Playgroud)加载doctrine库:通过将Doctrine库添加到application/config/autoload.php文件中的数组来自动加载它:
'$ autoload ['libraries'] = array('doctrine');`
或使用以下方法在任何其他库中手动将其加载到控制器中:
$this->load->library('doctrine');
如果你安装了Doctrine.php applications/third_party,你会使用:
$autoload[‘libraries’] = array('third_party/doctrine');
要么
$this->load->library('third_party/doctrine');
一个例子控制器中提供以下接下来会发生什么.
设置命令行工具
Doctrine附带了许多在开发过程中非常有用的命令行工具.
检查Doctrine.php文件中是否存在这些行,以加载Symfony类以使用命令行工具(以及YAML映射文件):
$symfonyClassLoader = new ClassLoader('Symfony', APPPATH.'third_party/Doctrine');
$symfonyClassLoader->register();
Run Code Online (Sandbox Code Playgroud)
您需要将应用程序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);
?>
Run Code Online (Sandbox Code Playgroud)
现在通过PHP命令行运行此脚本,并且应该看到可用的命令列表.
php cli-doctrine.php
Run Code Online (Sandbox Code Playgroud)
从数据库生成映射类:
php cli-doctrine.php orm:convert-mapping --from-database annotation models/Entities
Run Code Online (Sandbox Code Playgroud)
如果您收到此错误: 致命错误:调用未定义的函数Doctrine\Common\Cache\apc_fetch() 安装PHP的APC扩展:
sudo apt-get install php-apc
sudo /etc/init.d/apache2 restart
Run Code Online (Sandbox Code Playgroud)
对于生产模式:Doctrine建议在Doctrine.php中更改以下设置: - 使用像APC这样的真实缓存系统 - 禁用EchoSqlLogger
- 关闭autoGenerateProxyClasses
下一步是什么
要在CI中使用Doctrine,请从控制器中调用它,例如:
应用/控制器/ 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;
}
Run Code Online (Sandbox Code Playgroud)
但是,在执行任何Doctrine之前,必须先将数据库表映射到Doctrine"实体".在这里了解如何:https://www.doctrine-project.org/projects/doctrine-orm/en/2.6/tutorials/getting-started.html
| 归档时间: |
|
| 查看次数: |
8272 次 |
| 最近记录: |