douzhen1234 2016-01-27 19:01
浏览 79

如何在Doctrine 2中使用Proxies

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

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. 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 ?

  • 写回答

1条回答 默认 最新

  • douchenbiao0916 2016-01-28 03:49
    关注

    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

    评论

报告相同问题?

悬赏问题

  • ¥15 mmocr的训练错误,结果全为0
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀