繁体   English   中英

在Doctrine中使用自定义存储库

[英]Using custom Repository in Doctrine

我正在使用教义2与codeigniter。 我已经使用作曲家安装和配置了学说。 尝试访问我的存储库时,出现异常。 我列出了详细信息-

我的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;





}

尝试将此行更改为

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

这个

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

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

希望这可以帮助。

干杯!

我在我的library / doctrine.php中添加了以下内容,它有效-

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM