murze
murze

Reputation: 4103

Doctrine2: how to solve left join not being generated

consider these two entities:

/** @Entity  */
class String {
    /** @Id @Column(type="integer")
     * @GeneratedValue
     */
    public $id;

    /** @Column(length=255) */
    public $name;

    /**
     * @OneToMany(targetEntity="StringTranslation", mappedBy="owner") */
    public $translations;
}

/** @Entity */
class StringTranslation {
    /** @Id @Column(type="integer")
     * @GeneratedValue
     */
    public $id;

    /* @ManyToOne(targetEntity="String", inversedBy="translations")
     * @JoinColumn(name="foreignId", referencedColumnName="id") */
    public $owner;

    /** @Column(length=2) */
    public $lang;

    /** @Column(length=255) */
    public $translation;
}

When I get the query using

$query = qb()->select(array('s', 't'))
    ->from('String', 's')
    ->leftJoin('s.translations', 't')
    ->getQuery()
    ->getSQL();

I get this result:

SELECT s0_.id AS id0, s0_.name AS name1, s1_.id AS id2, s1_.foreign_id AS foreign_id3, s1_.lang AS lang4, s1_.translation AS translation5 FROM String s0_ LEFT JOIN

Why is the left join not being generated?

When i try to get the result of the query with

$query = qb()->select(array('s', 't'))
    ->from('String', 's')
    ->leftJoin('s.translations', 't')
    ->getQuery()
    ->getArrayResult();

this error comes up:

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax

How can this problem be solved?

Upvotes: 1

Views: 455

Answers (1)

murze
murze

Reputation: 4103

Found it! It all boils down to a typo. Annotations must start with /** and not with /*. Changing the line

/* @ManyToOne(targetEntity="String", inversedBy="translations")

to

/** @ManyToOne(targetEntity="String", inversedBy="translations")

solved the problem!

Upvotes: 1

Related Questions