如何在 CodeIgniter 3 中安装 Doctrine [英] How to install Doctrine in CodeIgniter 3
问题描述
The official guide is not complete, and others are for CI2.
所以我给你一个我自己检查过的教程.
So i give you a tutorial i myself checked is working.
我知道鼓励用户回答他们自己的问题.
推荐答案
安装 Doctrine
(以下说明修改自:Doctrine 2 ORM 的文档 - 安装和配置)
Doctrine 可以通过 Composer 安装:
Doctrine can be installed with Composer:
从您的命令行(例如 Windows:开始 > cmd),切换到应安装文件的文件夹(例如 htdocs/my_project).
From your command line (e.g. Windows: Start > cmd), change to the folder where the files should be installed (e.g. htdocs/my_project).
使用 Composer 安装文件:
Use Composer to install the files:
一个.运行 C:>composer 安装学说/orm
或:
B.在您的 composer.json 文件中定义以下要求:
b. Define the following requirement in your composer.json file:
{
"require": {
"doctrine/orm": "*"
}
}
然后从命令行调用 composer install
.
Composer 将安装一个文件夹 vendor
,其中包含许多子文件夹和数百个 php 文件.将此 vendor
文件夹移动到您的 CodeIgniter 应用程序树中.为简单起见,您可以将其放在这里:
Composer will install a folder vendor
with numerous sub-folders and several hundred php files. Move this vendor
folder into your CodeIgniter application tree. For simplicity, you could put it here:
/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
(见下文)中,您只需:
then in your library file Doctrine.php
(see below) you would just:
require_once FCPATH . 'vendor/autoload.php'; // FCPATH is a CI constant specifies the path to the front controller.
您还可以将 vendor
中包含的所有文件夹和文件安装到其他地方,例如 third_party
并相应地调整您的 Doctrine.php.
You could also install all the folders and files contained inside vendor
elsewhere, say in third_party
and adjust your Doctrine.php accordingly.
与 CodeIgniter 集成
(以下说明修改自:Doctrine2 ORM 的文档 - 与 CodeIgniter 集成)
创建您的 Doctrine 库:在您的文件夹
system/application/libraries
中,创建一个名为Doctrine.php
的文件并将以下代码复制/粘贴到文件.这将成为 Doctrine2 实体管理器的包装器/引导程序.
Create your Doctrine library: In your folder
system/application/libraries
, create a file namedDoctrine.php
and copy/paste the following code into the file. This is going to be your wrapper/bootstrap for the Doctrine2 entity manager.
您的 Doctrine.php 库文件应如下所示(您可以根据需要对其进行自定义):
Your Doctrine.php library file should look like this (you may customize it to your needs):
<?php
/**
* Doctrine 2.4 bootstrap
*
*/
use DoctrineCommonClassLoader,
DoctrineORMConfiguration,
DoctrineORMEntityManager,
DoctrineCommonCacheArrayCache,
DoctrineDBALLoggingEchoSQLLogger;
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 DoctrineCommonCacheArrayCache;
else:
// set up caching with APC for production mode
$cache = new DoctrineCommonCacheApcCache;
endif;
$config->setMetadataCacheImpl($cache);
$config->setQueryCacheImpl($cache);
// set up annotation driver
$driver = new DoctrineORMMappingDriverPHPDriver(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);
}
}
加载 Doctrine 库:要么通过将其添加到 application/config/autoload.php
文件中的数组来自动加载 Doctrine 库:
Load the doctrine library: either autoload your Doctrine library by adding it to the array in your application/config/autoload.php
file:
'$autoload['libraries'] = array('doctrine');`
'$autoload[‘libraries’] = array(‘doctrine’);`
或者像任何其他库一样手动在控制器中加载它:
or load it manually in your controller like any other library by using:
$this->load->library('doctrine');
如果您在 applications/third_party
中安装了 Doctrine.php,您将使用:
If you installed Doctrine.php in applications/third_party
, you would use:
$autoload['libraries'] = array('third_party/doctrine');
或
$this->load->library('third_party/doctrine');
下面的下一步中提供了一个示例控制器.
An example controller is provided below in What's next.
设置命令行工具
Doctrine 附带了许多在开发过程中非常有用的命令行工具.
Doctrine ships with a number of command line tools that are very helpful during development.
检查 Doctrine.php 文件中是否存在这些行,以加载 Symfony 类以使用命令行工具(以及 YAML 映射文件):
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();
您需要通过在应用程序目录中创建一个包含以下内容的 cli-doctrine.php 文件,将您的应用程序 EntityManager 注册到控制台工具以使用这些任务:
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 SymfonyComponentConsoleHelperHelperSet(array(
'db' => new DoctrineDBALToolsConsoleHelperConnectionHelper($em->getConnection()),
'em' => new DoctrineORMToolsConsoleHelperEntityManagerHelper($em)
));
DoctrineORMToolsConsoleConsoleRunner::run($helperSet);
?>
现在通过 PHP 命令行运行此脚本,应该会看到可用的命令列表.
Now run this script through the PHP command-line and should see a list of commands available to you.
php cli-doctrine.php
从数据库生成映射类:
php cli-doctrine.php orm:convert-mapping --from-database annotation models/Entities
如果您收到此错误:致命错误:调用未定义的函数 DoctrineCommonCacheapc_fetch()为 PHP 安装 APC 扩展:
if you get this error: Fatal error: Call to undefined function DoctrineCommonCacheapc_fetch() install the APC extension for PHP:
sudo apt-get install php-apc
sudo /etc/init.d/apache2 restart
<小时>
对于生产模式:Doctrine 建议更改 Doctrine.php 中的以下设置:- 使用真正的缓存系统,如 APC- 禁用 EchoSqlLogger
- 关闭 autoGenerateProxyClasses
For production mode:
Doctrine recommends changing the following settings in Doctrine.php:
- use a real caching system like APC
- disable EchoSqlLogger
- turn off autoGenerateProxyClasses
接下来是什么
要在 CI 中使用 Doctrine,请从控制器中调用它,例如:
To use Doctrine in CI, call it from a controller, for example:
application/controllers/my_controller.php:
application/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
", $product->getName());
endforeach;
}
然而,在做任何 Doctrine 的东西之前,你必须首先将你的数据库表映射到 Doctrine实体".在此处了解操作方法:https://www.doctrine-project.org/projects/doctrine-orm/en/2.6/tutorials/getting-started.html
Before doing any Doctrine stuff, however, you must first map your database tables to Doctrine "entities". Learn how here: https://www.doctrine-project.org/projects/doctrine-orm/en/2.6/tutorials/getting-started.html
这篇关于如何在 CodeIgniter 3 中安装 Doctrine的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!