首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >理论与CodeIgniter的集成

理论与CodeIgniter的集成
EN

Stack Overflow用户
提问于 2013-06-15 09:09:22
回答 7查看 6K关注 0票数 6

我已经成功地安装了最新版本的CodeIgniter,并且基本的MVC模式可以工作。我注意到的问题是,当涉及到查询时,CI自然不允许准备语句。因此,我决定从GitHub下载Doctrine 1。我对Doctrine非常陌生,需要一些帮助将它与CI集成在一起,所以我遵循了这个教程

在我的一个控制员里,我

代码语言:javascript
运行
复制
$this->load->library('doctrine');
$this->em = $this->doctrine->em;

但是,当我在浏览器中加载视图时,会发现读取错误

消息: require_once(/Applications/MAMP/htdocs/CodeIgniter/application/libraries/Doctrine/Common/ClassLoader.php):未能打开流:没有这样的文件或目录

在进一步查看从GitHub下载的Doctrine之后,这里似乎没有一个名为“公共”的文件夹。我对CI很陌生,尤其是教条。有人有什么建议能帮我完成这个任务吗?另外,是否可以使用MySQLi驱动程序而不是带有Doctrine的PDO驱动程序?

EN

回答 7

Stack Overflow用户

回答已采纳

发布于 2013-06-15 16:54:55

直接从GitHub下载Doctrine不包括其他依赖项。这些是由作曲家管理的。如果您查看composer.json文件中的内容,可以看到这些依赖项。如果您想手动安装它们,它们是:

  • 学说/共同
  • 学说/启示
  • 理论/缓存
  • 学说/收藏
  • 理论/词汇
  • 学说/说明
  • 学说/dbal
  • symfony/控制台

我相信这就是他们的全部。您必须将这些文件合并到它们相应的目录中,因为它们遵循PSR-0标准,以实现类的自动加载。

或者,使用下面的composer.json文件与Composer一起安装Doctrine 2,任何其他依赖项都将自动安装。然后是与CodeIgniter集成

代码语言:javascript
运行
复制
{
    "minimum-stability": "stable",
    "require": {
        "doctrine/orm": "2.3.*"
    }
}

通过添加一行代码来编辑index.php应用程序的CodeIgniter文件,在需要CodeIgniter内核之前包含自动加载程序文件。

代码语言:javascript
运行
复制
require_once BASEPATH.'../vendor/autoload.php';

require_once BASEPATH.'core/CodeIgniter.php';

另外,如果使用Composer安装,请使用这个已编辑的引导程序版本作为application/libraries/Doctrine.php的内容,这对我来说是有效的。

代码语言:javascript
运行
复制
<?php

use Doctrine\Common\ClassLoader,
    Doctrine\ORM\Tools\Setup,
    Doctrine\ORM\EntityManager;

class Doctrine
{
    public $em;

    public function __construct()
    {
        // Load the database configuration from CodeIgniter
        require APPPATH . 'config/database.php';

        $connection_options = array(
            'driver'        => 'pdo_mysql',
            'user'          => $db['default']['username'],
            'password'      => $db['default']['password'],
            'host'          => $db['default']['hostname'],
            'dbname'        => $db['default']['database'],
            'charset'       => $db['default']['char_set'],
            'driverOptions' => array(
                'charset'   => $db['default']['char_set'],
            ),
        );

        // With this configuration, your model files need to be in application/models/Entity
        // e.g. Creating a new Entity\User loads the class from application/models/Entity/User.php
        $models_namespace = 'Entity';
        $models_path = APPPATH . 'models';
        $proxies_dir = APPPATH . 'models/Proxies';
        $metadata_paths = array(APPPATH . 'models');

        // Set $dev_mode to TRUE to disable caching while you develop
        $config = Setup::createAnnotationMetadataConfiguration($metadata_paths, $dev_mode = true, $proxies_dir);
        $this->em = EntityManager::create($connection_options, $config);

        $loader = new ClassLoader($models_namespace, $models_path);
        $loader->register();
    }
}

注意: CodeIgniter的第3版一旦发布,就可以在Composer中安装,但是版本2不能安装。

票数 12
EN

Stack Overflow用户

发布于 2015-10-31 22:56:40

对于那些寻找教程将Doctrine 2与CodeIgniter集成在一起的人来说,这个问题和其他答案已经过时了(对于CI 2)。这是我为CI 3编写的一个新教程,我检查了一下:

如何在CodeIgniter 3中安装Doctrine 2

我在这里重复一遍。

安装原则

原则2 ORM的文档.安装和配置

原则可以与作曲家一起安装。在composer.json文件中定义以下要求:

代码语言:javascript
运行
复制
{
    "require": {
        "doctrine/orm": "*"
    }
}

然后从命令行调用composer安装。

与CodeIgniter的集成

理论2 ORM的文档-与CodeIgniter的集成

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

创建您的Doctrine CodeIgniter库

现在,下面是您的Doctrine.php文件的样子。根据您的需要定制它。

代码语言:javascript
运行
复制
<?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映射文件):

代码语言:javascript
运行
复制
$symfonyClassLoader = new ClassLoader('Symfony', APPPATH.'third_party/Doctrine');
$symfonyClassLoader->register();

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

代码语言:javascript
运行
复制
 <?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命令行运行这个脚本,应该可以看到一个命令列表。

代码语言:javascript
运行
复制
php cli-doctrine.php

从数据库生成映射类:

代码语言:javascript
运行
复制
php cli-doctrine.php orm:convert-mapping --from-database annotation models/Entities

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

代码语言:javascript
运行
复制
sudo apt-get install php-apc
sudo /etc/init.d/apache2 restart

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

票数 2
EN

Stack Overflow用户

发布于 2015-05-14 07:36:37

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17121997

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档