qbbr
qbbr

Reputation: 51

Doctrine 2 UPDATE with LEFT JOIN

SELECT - all right, no errors

$em = $this->get('doctrine.orm.entity_manager');

$query = $em->createQuery("
    SELECT c
    FROM MyDemoBundle:Category c
    LEFT JOIN c.projects p
    WHERE c.isActive = true
    AND p.id = 1
");
$result = $query->getResult();

UPDATE - exception [Semantical Error]

$query = $em->createQuery("
    UPDATE MyDemoBundle:Category c
    LEFT JOIN c.projects p
    SET c.isActive = false
    WHERE p.id = ?1
");
$query->setParameter(1, $id);
$query->execute();

Upvotes: 5

Views: 6923

Answers (1)

beberlei
beberlei

Reputation: 4337

LEFT JOIN, or JOINs in particular are only supported in UPDATE statements of MySQL. DQL abstracts a subset of common ansi sql, so this is not possible. Try with a subselect:

UPDATE MyDemoBundle:Category c SET c.isActive = false WHERE ?1 MEMBER OF c.projects;

(MEMBER OF is actually turning into a subselect here). I am not 100% sure if this works, but it is more likeli than the join.

Upvotes: 1

Related Questions