如何在CodeIgniter中安装Doctrine 3 [英] How to install Doctrine in CodeIgniter 3

查看:270
本文介绍了如何在CodeIgniter中安装Doctrine 3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

官方指南不是完成,其他用于CI2。

The official guide is not complete, and others are for CI2.

所以我给你一个我自己检查的教程正在工作。

So i give you a tutorial i myself checked is working.

我知道,鼓励用户回答自己的问题

推荐答案

安装原则

Doctrine 2 ORM的文档 - 安装和配置

可以使用<一个href =http://www.getcomposer.org/ =nofollow>作曲者。
在composer.json文件中定义以下要求:

Doctrine can be installed with Composer. Define the following requirement in your composer.json file:

{
    "require": {
        "doctrine/orm": "*"
    }
}

然后从命令行调用composer install。

Then call composer install from your command line.

与CodeIgniter集成

教义2 ORM的文档 - 与CodeIgniter集成

以下是步骤:
将php文件添加到名为Doctrine.php的系统/应用程序/库文件夹中。这将是D2实体管理器的包装/引导。
将Doctrine文件夹(包含Common,DBAL和ORM的文件夹)放在third_party文件夹中。
如果需要,打开你的config / autoload.php文件,并自动加载你的Doctrine库: $ autoload ['libraries'] = array('doctrine');

创建您的Doctrine CodeIgniter库

现在,这里是你的教义。 php文件应该看起来像。根据您的需要自定义。

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.

检查Doctrine.php文件中是否存在这些行,以加载使用命令行工具(和YAML映射文件)的Symfony类:

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();

您需要将应用程序EntityManager注册到控制台工具,以通过创建cli来使用任务-doctrine.php文件在应用程序目录中,具有以下内容:

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);

 ?>

现在,通过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

如果您收到此错误:
致命错误:调用未定义的函数Doctrine\Common\Cache\apc_fetch()
为PHP安装APC扩展名:

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

对于生产模式,您将需要使用像APC这样的真正的缓存系统,摆脱 EchoSqlLogger ,并在Doctrine.php中关闭 autoGenerateProxyClasses

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中安装Doctrine 3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆