【问题标题】:Using custom Repository in Doctrine在 Doctrine 中使用自定义存储库
【发布时间】:2014-08-06 10:35:20
【问题描述】:

我正在使用带有 codeigniter 的 Doctrine 2。我已经使用作曲家安装和配置了学说。当我试图访问我的存储库时,我得到了一个异常。我正在列出详细信息 -

我的 composer.json 文件是 -

{
    "minimum-stability": "stable",
    "autoload": {
        "psr-4": {
            "" : "application",
            "Myapp\\Entity\\": "application/models/entities/",
            "Myapp\\Repository\\": "application/models/repositories/"
        }
    },
    "require": {
        "doctrine/common": "2.4.*",
        "doctrine/dbal": "2.4.*",
        "doctrine/orm": "2.4.*"
    }
}

我创建了 application/libraries/doctrine.php 作为 -

<?php

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

class Doctrine
{
    private $em;

    public function __construct()
    {
        $app_path = getcwd()."/application/";
        // 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'        => 'mydb',
            '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
        $entity_path = $app_path. 'models/entities';

        // Set $dev_mode to TRUE to disable caching while you develop
        $isDevMode = true;
        $config = Setup::createAnnotationMetadataConfiguration(array($entity_path), $isDevMode);

        $this->em = EntityManager::create($connection_options, $config);
    }

    public function getEntityManager() {
        return $this->em;
    }
}

我的控制器代码是-

class Home extends CI_Controller{
    function __construct()
    {
        global $page;
        parent::__construct();
    }

    function index()
    {
        global $page;

        $x = $this->doctrine->getEntityManager()->getRepository('Myapp\\Entity\\MyEntity');
        var_dump($x);
        die;
    }
}

我收到以下错误 -

Fatal error: Uncaught exception 'Doctrine\ORM\Mapping\MappingException' with message 'Class "Myapp\Entity\MyEntity" is not a valid entity or mapped super class.' in /home/piyusht/Projects/XXX/YYY/vendor/doctrine/orm/lib/Doctrine/ORM/Mapping/MappingException.php on line 336

我的实体类如下 -

<?php
namespace Myapp\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * MyEntity
 *
 * 
 * 
 * @ORM\Entity(repositoryClass="Myapp\\Repository\\MyEntityRepository")
 */
class MyEntity
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer", nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $id;





}

【问题讨论】:

  • 您也可以粘贴您的实体文件吗?干杯!
  • 感谢@AnjanaSilva,我也添加了实体类。
  • 在下面查看我的答案 :)

标签: php codeigniter doctrine-orm doctrine


【解决方案1】:

尝试将此行更改为

$config = Setup::createAnnotationMetadataConfiguration(array($entity_path), $isDevMode);

这个

$config = Setup::createAnnotationMetadataConfiguration(array($entity_path), $isDevMode, null, null, false);

它很可能使用 SimpleAnnotationReader,因此您需要使用 AnnotationReader 将命名空间前缀用作 ORM\。

希望这会有所帮助。

干杯!

【讨论】:

    【解决方案2】:

    我在我的 library/doctrine.php 中添加了以下内容,它起作用了 -

    $driver = $config->newDefaultAnnotationDriver(
                    APPPATH . '/models/entities', false
            );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-10-28
      • 1970-01-01
      • 1970-01-01
      • 2016-08-16
      • 2018-02-09
      • 2017-08-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多