php - Doctrine2 QB OneToMany 连接上的语义错误

标签 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');

实体类(class)中的相关代码如下所示:

    /**
     * @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 之后停止!

任何帮助将不胜感激,因为我真的不知道我做错了什么,也不知道如何解决这个问题。我在互联网上搜索了类似的错误,但到目前为止还没有运气。

最佳答案

尝试至少选择在 select 数组内的 StudyCourses 表中形成 leftJoin 所需的列。首先尝试获取诸如“select( array('n', 't', 'c', 's') )”之类的所有内容。有时,如果您未选择该列,它就会中断。如果这有效,不要获取 StudyCourses 表中的所有列,只需获取 select 中的相关列以形成 leftJoin,例如“s.id”。

希望这有帮助。

干杯!

关于php - Doctrine2 QB OneToMany 连接上的语义错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23929593/

相关文章:

mysql - 将 HQL 与 MySQL 结合使用,如何在分组依据之前对结果集进行排序,以便选择正确的记录?

Symfony2 + Doctrine2 不缓存连接实体的结果

php - 自动将 Voter 应用到 Controller 中的多种方法

php - insert into table where column increments by 1 与相同数据的其他行相比

php - substr 以逗号生效

PHP - 在数据库中存储 session

php - ZF2 Doctrine 与 objectSelect 获得多对多关系

mysql - Entity 和 MySQL (Symfony2) 中的 Doctrine 数组类型

php - mysql查询多张表并统计行数

php - 通过xpath获取div的HTML内容