Doctrine2 QB Sementical error on OneToMany join

67 views Asked by At

I am trying to create a query using the 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');

The relevant code in entity Course looks like:

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

The relevant code in entity StudyCourse looks like:

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

Now, when I try to run my query, I'm getting a semantic error near ''. I figured printing the SQL created by Doctrine would give me better information about this error, but in fact it is:

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 

It just stops after the LEFT JOIN!

Any help would be appreciated, as I really don't know what I'm doing wrong, or how to solve this issue. I searched the internet for similar errors, but no luck so far.

1

There are 1 answers

0
Anjana Silva On

Try at least selecting columns which are required to form the leftJoin in studyCourses table, inside select array. First try and get everything such as "select( array('n', 't', 'c', 's') )". Sometimes it breaks if you haven't selected the column. If this worked, don't take all columns which are inside studyCourses table, just get the relevant column in select in order to form the leftJoin, such as 's.id'.

Hope this helps.

Cheers!