Doctrine 2 inject data to loaded models

纵然是瞬间 提交于 2019-12-11 15:34:36

问题


I have a Doctrine 2 model which has a relation to a file system model (MogileFS) which I've implemented using a model/mapper approach. What I'm trying to accomplish is to lazy load the non-doctrine model from a Doctrine 2 entity, and inject a mapper object into this model while doing so.

Example:

use Doctrine\ORM\Tools\Pagination\Paginator;
$dql = "SELECT p, c FROM BlogPost p JOIN p.comments c";
$query = $entityManager->createQuery($dql)
                   ->setFirstResult(0)
                   ->setMaxResults(100);

$paginator = new Paginator($query, $fetchJoin = true);

$c = count($paginator);
foreach ($paginator as $post) {
  // TODO Should use injected mapper to do find() 
  // and lazy load model when not set
    echo $post->getThumbnailFileModel() . "\n";
}

How can I set up an entity loading hook, that will inject my modelMapper into the entity in order for the lazy loading to work?


回答1:


I managed to solve this by using @postLoad event listener.

For reference, see: http://docs.doctrine-project.org/projects/doctrine-orm/en/2.0.x/reference/events.html



来源:https://stackoverflow.com/questions/9437763/doctrine-2-inject-data-to-loaded-models

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!