Lamin L. Janneh
Lamin L. Janneh

Reputation: 95

how to join two table in symfony2 DQL query

$var=$em->createQuery('SELECT a, p FROM AdminBundle:Appraisal a
       LEFT JOIN AdminBundle:AppResult p ON a.id=p.Appraisalid');

I want to query between two table but this is not working

Upvotes: 0

Views: 920

Answers (1)

M Gholami
M Gholami

Reputation: 971

you must use mapping associations between objects. one to many OR one to one OR many to many? if AdminBundle:Appraisal has many AdminBundle:AppResult (ONE TO MANY), you must mapp this two entity like this: in Appraisal.php make this changes:

/**
 * @ORM\OneToMany(targetEntity="AppResult", mappedBy="raisal")
 **/
private $results;
// ...

public function __construct() {
    $this->results = new Doctrine\Common\Collections\ArrayCollection();
}

and in AppResult.php make this changes:

 /**
     * @ORM\ManyToOne(targetEntity="Appraisal", inversedBy="results")
     * @ORM\JoinColumn(name="result_id", referencedColumnName="id")
     **/
    private $raisal;

now update your entities :

php app/console doctrine:generate:entities AdminBundle:Appraisal

php app/console doctrine:generate:entities AdminBundle:AppResult

update your schema: php app/console doctrine:schema:update --force

now you can make query like this:

$var=$em->createQuery('SELECT a, p FROM AdminBundle:Appraisal a
       LEFT JOIN a.results p');

hope its help U. full help can be found here

Upvotes: 1

Related Questions