Pawel
Pawel

Reputation: 1692

SQL query in symfony2 and doctrine2

i'm trying to create a SQL query in Doctrine 2 (Symfony2 Framework), and i need some help. I have two Tables Movie and Person in relation many to many in table Movie_Person.

I want to get a Persons who plays in film. So clear SQL should look like:

SELECT p.* FROM Person p WHERE EXISTS 
                (SELECT m.* FROM Movie_Person m WHERE p.id = m.person_id)

How to write this in Symfony2 + Doctrine2 ?

Upvotes: 0

Views: 5131

Answers (1)

Jakub Zalas
Jakub Zalas

Reputation: 36191

Example taken from the docs:

$query = $em->createQuery('SELECT u.id 
                             FROM CmsUser u 
                            WHERE EXISTS (SELECT p.phonenumber 
                                            FROM CmsPhonenumber p 
                                           WHERE p.user = u.id)');
$ids = $query->getResult();

Important note: Remember, this is NOT SQL. It's DQL. The main difference is you operate on objects and properties as opposed to database tables and fields.

Upvotes: 6

Related Questions