dpbf62565
dpbf62565
2018-09-02 12:35
浏览 29
已采纳

Doctrine:从原始SQL中保存模型

I have the following custom query. I know its a simple one, so it can also be used as a DQL, but I have more complex ones too. But I want to know the way, how to do it even with more complex queries.

select j.*
from `shop`.`jobs` j
-- 2 joins
where j.`active` = true 
order by j.`priority` desc, j.`created` asc 

Sure have a Job model.

What I want:

An array of instances from the class Job, using the a raw sql. Like this:

array (6) {
    [0] => Job#12 (8) {
      ...
    }
    [1] => Job#13 (8) {
      ...
    }
    [2] => Job#14 (8) {
      ...
    }
    [3] => Job#16 (8) {
      ...
    }
    [4] => Job#17 (8) {
      ...
    }
    [5] => Job#18 (8( {
      ...
    }
}
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongshijiao2363
    dongshijiao2363 2018-09-03 06:06
    已采纳

    Found it! Through the EntityManager's createNativeQuery function. And for the RSM I need to use the ResultSetMappingBuilder class.

    $rsm = new \Doctrine\ORM\Query\ResultSetMappingBuilder($this->getEntitiyManager());
    $rsm->addRootEntityFromClassMetadata(\Path\To\Model::class, 'alias');
    
    $nativeQuery = $this->getEntitiyManager()->createNativeQuery('-- query--', $rsm);
    
    $nativeQuery->getResult();
    
    点赞 评论

相关推荐