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

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 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();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥88 实在没有想法,需要个思路
  • ¥15 MATLAB报错输入参数太多
  • ¥15 python中合并修改日期相同的CSV文件并按照修改日期的名字命名文件
  • ¥15 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败