meteor
meteor

Reputation: 656

Can't use JOIN with related entities in doctrine2

I was guided by this, and I can't understand what's going wrong.

My entities:

/**
 * @ORM\Entity
 * @ORM\Table(name="term")
 */
class Term {
    /**
     * @ORM\Column(type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;
    /**
     * @ORM\OneToMany(targetEntity="Description", mappedBy="term")
     **/ 
    private $description;

    //....
}

/**
 * @ORM\Entity
 * @ORM\Table(name="description")
 */
class Description {

    /**
     * @ORM\Column(type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;
    /**
     * @Orm\ManyToOne(targetEntity="term", inversedBy="description")
     * @Orm\JoinColumn(name="term_id", referencedColumnName="id")
     **/  
    private $term;
    /**
     * @ORM\Column(type="string", length=8)
     */
    private $normativity;
    //...
}

I need to get terms and filter terms descriptions by one of it fields (normativity in example).

I tried this:

$query = $this->getDoctrine()->getEntityManager()
    ->createQuery("
        SELECT term, desc FROM myTerminologyBundle:Term term
        JOIN term.description desc
        WHERE term.word LIKE :r_word' and desc.normativity IN :norm"         
    )->setParameter('r_word', '%'.$word.'%')->setParameter('norm', array());

and I get the following exceptions:

[Syntax Error] line 0, col 30: Error: Expected IdentificationVariable | ScalarExpression | AggregateExpression | FunctionDeclaration | PartialObjectExpression | "(" Subselect ")" | CaseExpression, got 'desc'

Upvotes: 0

Views: 141

Answers (3)

meteor
meteor

Reputation: 656

There are reserved word desc in SQL. I change it to another and it works.

Upvotes: 0

Suresh Kumar Amrani
Suresh Kumar Amrani

Reputation: 935

Use dql

$query= $this->getEntityManager()
                        ->createQueryBuilder()
                        ->select('term.id as id,term.name as name')
                        ->from('FROM myTerminologyBundle:Term ', 'term')
                        ->innerJoin('term.Description ', 'desc')
                        ->Where('term.word LIKE=:term')
                        ->setParameters(array('term' => $term))
                         ->orWhere('desc.normativity  IN(:category_id)')
                        ->setParameter('category_id',$category_id)

                        ->getQuery();
             return $query->getResult();

Upvotes: 0

LorenzoR
LorenzoR

Reputation: 421

Try something like this:

createQuery("
    SELECT term
    FROM myTerminologyBundle:Term term
    INNER JOIN myTerminologyBundle:Description desc
    WHERE term.word LIKE :r_word' and desc.normativity IN :norm"         
)

Upvotes: 1

Related Questions