Doctrine2 QB OneToMany加入时的Sementical错误

时间:2014-05-29 09:05:09

标签: php mysql doctrine-orm doctrine-query zfdoctrine

我正在尝试使用Doctrine2 QueryBuilder创建查询。

    $qb = $this->getObjectManager()->createQueryBuilder();
    $qb->select( array('n', 't', 'c') )
        ->from('Application\Entity\Notification', 'n')
        ->leftJoin('n.notificationType', 't')
        ->leftJoin('n.course', 'c')
        ->leftJoin('c.studyCourses', 's');

实体课程中的相关代码如下:

    /**
     * @ORM\OneToMany(targetEntity="StudyCourses", mappedBy="Course", cascade={"all"})
     */
    protected $studyCourses;

实体StudyCourse中的相​​关代码如下:

    /**
     * @ORM\ManyToOne(targetEntity="Course", inversedBy="studyCourses")
     * @ORM\JoinColumn(name="Course", referencedColumnName="Id", nullable=true)
     */
    protected $course;  

现在,当我尝试运行查询时,我在''附近遇到语义错误。我想打印由Doctrine创建的SQL会给我更好的关于这个错误的信息,但事实上它是:

SELECT n0_.Id AS Id0, n0_.Timestamp AS Timestamp1, n0_.TitleHtml AS TitleHtml2, n0_.ContentHtml AS ContentHtml3, n1_.Id AS Id4, n1_.Created AS Created5, n1_.Updated AS Updated6, n1_.Name AS Name7, c2_.Id AS Id8, c2_.Created AS Created9, c2_.Updated AS Updated10, c2_.Name AS Name11, c2_.Description AS Description12, c2_.Code AS Code13, c2_.ObjectId AS ObjectId14, c2_.IsActive AS IsActive15, n0_.NotificationType AS NotificationType16, n0_.User AS User17, n0_.Department AS Department18, n0_.Study AS Study19, n0_.Course AS Course20, n0_.Category AS Category21, c2_.Language AS Language22 FROM Notification n0_ LEFT JOIN NotificationType n1_ ON n0_.NotificationType = n1_.Id LEFT JOIN Course c2_ ON n0_.Course = c2_.Id LEFT JOIN 

在LEFT JOIN之后停止!

任何帮助都会受到赞赏,因为我真的不知道我做错了什么,或者如何解决这个问题。我在互联网上搜索了类似的错误,但到目前为止还没有运气。

1 个答案:

答案 0 :(得分:0)

尝试至少选择在studyCourses表中形成leftJoin所需的列,在select数组中。首先尝试获取所有内容,例如"选择(数组(' n',' t',' c',' s' ))"。如果您没有选择该列,有时会中断。如果这样做有效,请不要将所有列放在studyCourses表中,只需选择相关列以形成leftJoin,例如' s.id'。

希望这有帮助。

干杯!