简体   繁体   English

如何在教义2中使用代理

[英]How to use Proxies in Doctrine 2

I have the following configuration 我有以下配置

 $isDevMode = \Config::get("DEBUG");
self::$config = Setup::createAnnotationMetadataConfiguration(array(APP_PATH.'models'), $isDevMode, APP_PATH.'proxies');

// naming strategy
$namingStrategy = new \DoctrineExtensions\CustomNamingStrategy();
self::$config->setNamingStrategy($namingStrategy);

// database configuration parameters
self::$conn = array(
    'url' => \Config::get('DB_DSN')
);

self::$evm = new \Doctrine\Common\EventManager;

// Table Prefix
$tablePrefix = new \DoctrineExtensions\TablePrefix(\Config::get('TABLE_PREFIX'));
self::$evm->addEventListener(Events::loadClassMetadata, $tablePrefix);
self::$entityManager = EntityManager::create(self::$conn, self::$config, self::$evm);

From Doctrine 2 Documentation which I understand, this should be generate Proxy classes in APP_PATH.'proxies' folder ( when $isDevMode is true. This should happen automatically). 根据我了解的Doctrine 2文档,这应该是在APP_PATH.'proxies'文件夹中生成Proxy类(当$isDevMode为true时,应自动发生)。

Unfortunately it doesn't automatically generated classes. 不幸的是,它不会自动生成类。 I don't know what the reason. 我不知道是什么原因。 However I generated these classes manually by using the following command php vendor/bin/doctrine orm:generate-proxies . 但是,我使用以下命令php vendor/bin/doctrine orm:generate-proxies手动生成了这些类。 Classes are generated and already exist in the folder. 类已生成,并且已经存在于文件夹中。 In order to try Proxy classes i wrote the following lines: 为了尝试代理类,我写了以下几行:

$qq = $this->repositoryPages
->createQueryBuilder('p')
->select('p.title')
->where('p.id = :id')
->setParameter('id', 6)
->getQuery()->getResult();
var_dump($qq);

Unfortunately for me this returns an associative array 不幸的是,这返回了一个关联数组

array (size=1)
0 => 
   array (size=1)
     'title' => string 'Welcome' (length=7)

What is wrong and what I need to do to get the object ? 什么是错的,我需要怎么做才能得到该物体?

I think you forgot to add one setter 我想您忘记添加一个二传手

self::config->setAutoGenerateProxyClasses(true);

I forgot to add example: 我忘了添加示例:

$this->repositoryPages
    ->createQueryBuilder('p')
    ->where('p.id = :id')
    ->setParameter('id', 6)
    ->getQuery()->getResult();

Query builder documentation 查询构建器文档

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

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