As of Doctrine 2.1, the CASE WHEN statement is supported but there isn't a lot of documentation on it. My goal is to set a boolean value to tell if a photo has been favorited by a user:
->addSelect("CASE WHEN f.photo is NULL THEN false ELSE true END as is_favorited")
->leftJoin("p.favorites", 'f', 'WITH', 'f.owner = :viewer')
->orderBy("p.date_posted", "DESC")
->setParameters(array("owner" => $owner, "viewer" => $viewer));
But because my entities are being transformed into json by JMSSerializer, I would like to set the CASE WHEN result as a property on the entity.
->addSelect("CASE WHEN f.photo is NULL THEN false ELSE true END as p.is_favorited")
But unforunately Doctrine does not seem to like this:
[Syntax Error] line 0, col 65: Error: Expected Doctrine\ORM\Query\Lexer::T_FROM, got '.'
Is there an alternative to setting DQL created properties on an entity?
->addSelect('f.photo IS NOT NULL')not have the same effect (but perhaps not trip up Doctrine)? - Tomas CreemersCASEin()like(CASE WHEN f.photo is NULL THEN false ELSE true END) as is_favorited? - prodigitalson