1

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?

2
  • 1
    Have you validated your schema using orm:validate-schema? Dec 7, 2011 at 10:34
  • I'm new to doctrine so I'm figuring out how to run orm:validate schema. When I use the Schematool to generate schemasql it outputs array( [0] => 'CREATE TABLE String (id INT AUTO_INCREMENT NOT NULL, name VARCHAR(255) NOT NULL, PRIMARY KEY(id)) ENGINE = InnoDB' [1] => 'CREATE TABLE StringTranslation (id INT AUTO_INCREMENT NOT NULL, lang VARCHAR(2) NOT NULL, translation LONGTEXT NOT NULL, PRIMARY KEY(id)) ENGINE = InnoDB' ) The foreignId column in StringTranslation seems to be missing even though I specified it in de JoinColumn annotation
    – murze
    Dec 7, 2011 at 10:39

1 Answer 1

1

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!

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.