Doctrine createNativeQuery不返回任何结果

时间:2014-07-22 15:02:14

标签: symfony doctrine-orm

TL;博士

我有一个有效的SQL查询,它返回SQL命令行中的行,但不是通过Doctrine提交的。我认为问题在于我如何设置ResultSetMapper。我已经阅读了有关原生查询的docs但我必须遗漏一些东西。它是什么?

详细

查询

从SQL命令行运行以下查询会产生1行:

SELECT t.*
FROM 
  StateLogItem s NATURAL JOIN (
    SELECT 
      task_id, 
      MAX(
        COALESCE(updatedAt, createdAt)
      ) d, 
      MAX(id) id 
    FROM 
      StateLogItem 
    WHERE 
      dtype = 'transitionlogitem' 
    GROUP BY 
      task_id
  ) m 
  RIGHT JOIN Task t ON t.id = s.task_id 
WHERE  s.toState = 'started';

结果:

'20', 1, NULL, 'just because', '2014-07-18 10:49:00', NULL

使用ResultSetMapping

当我尝试在Symfony EntityRepository中通过Doctrine运行相同的查询时(如下所示),即使Symfony探查器指示正确的查询已成功运行,我也会得到零行。

public function findByActionStatus($status){
    $sql = "SELECT t.id, t.description, t.created_by, t.updated_by, t.createdAt, t.updatedAt
            FROM StateLogItem s
                NATURAL JOIN (
                    SELECT task_id, MAX(COALESCE(updatedAt,createdAt)) d, MAX(id) id
                    FROM StateLogItem
                    WHERE dtype = 'transitionlogitem'
                    GROUP BY task_id
                ) m
                RIGHT JOIN Task t ON t.id=s.task_id
            WHERE s.toState = :status";

    $em = $this->getEntityManager();

    $rsm = new ResultSetMapping();
    $rsm->addEntityResult('ACCQueueBundle:Task', 't');
    $rsm->addFieldResult('t','t.id','id');
    $rsm->addFieldResult('t','t.created_by','createdBy');
    $rsm->addFieldResult('t','t.updated_by','updatedBy');
    $rsm->addFieldResult('t','t.description','description');
    $rsm->addFieldResult('t','t.createdAt','createdAt');
    $rsm->addFieldResult('t','t.updatedAt','updatedAt');

    $rsm->addJoinedEntityResult('ACCQueueBundle:TransitionLogItem','s','t','transitions'); //tried with and without


    $query = $em->createNativeQuery($sql,$rsm);
    $query->setParameter('status', $status);

    return $query->getResult();

}

实体

Task实体如下所示:

namespace ACC\QueueBundle\Entity;

use Doctrine\ORM\Mapping as ORM;


use Symfony\Component\Validator\Constraints as Assert;
use Doctrine\Common\Collections\ArrayCollection;
use ACC\Traits\BlameableTrait;
use ACC\MainBundle\Entity\Traits\HasDocumentsTrait;
use Gedmo\Mapping\Annotation as Gedmo;
use Gedmo\Timestampable\Traits\TimestampableEntity;    


/**
 * Task
 * @ORM\Entity(repositoryClass="ACC\QueueBundle\Entity\TaskRepository")
 * @ORM\HasLifecycleCallbacks()
 *
 */
class Task {

    use HasDocumentsTrait;
    use BlameableTrait;
    use TimestampableEntity;

    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;


    /**
     * @var Transition
     *
     * @ORM\OneToMany(targetEntity = "TransitionLogItem", mappedBy = "task", orphanRemoval = true, cascade = {"persist"})
     * @ORM\OrderBy({"createdAt" = "ASC"})
     */
    protected $transitions;

    /**
     * @var Pause
     *
     * @ORM\OneToMany(targetEntity = "PauseLogItem", mappedBy = "task", orphanRemoval = true, cascade = {"persist"})
     * @ORM\OrderBy({"createdAt" = "ASC"})
     */
    protected $pauses;


    /**
     * @var string
     *
     * @ORM\Column(name="description", type="text")
     */
    protected $description;

    /**
     * @ORM\ManyToMany(targetEntity = "ACC\MainBundle\Entity\Document", cascade={"persist","remove"})
     * @ORM\JoinTable(name="Task_Document",
     *  joinColumns={@ORM\JoinColumn(name="task_id", referencedColumnName="id")},
     *  inverseJoinColumns={@ORM\JoinColumn(name="document_id", referencedColumnName="id", unique=true)})
     */
    protected $documents;

//etc.

规格:

  • PHP 5.5.14
  • Doctrine ORM 2.4.4
  • Symfony 2.5.2
  • symfony environment:dev
  • 34年5月5日 - MariaDB的日志

2 个答案:

答案 0 :(得分:8)

我找到了问题。它是由ResultSetMapping不理解我给它的查询字段名称引起的,因为我包含了表前缀。所以我可以不包括表前缀

即。将$rsm->addFieldResult('t','t.id','id');更改为$rsm->addFieldResult('t','id','id');

或者我可以为所有字段添加别名:

即。将SELECT t.id, t.description...更改为SELECT t.id AS t_id, t.description AS t_description...

答案 1 :(得分:0)

在我的情况下没有结果,因为在SQL查询中我没有选择“主要”实体(FROM子句中的那个)的“ id”列。