Storing "CASE WHEN" condition in Doctrine2 entity
doctrine-orm, mysql, php, symfony
Solution
There is no way to set property value in query directly. Instead, you can add not-mapped field into entity, and then walk through results and set it. In this case `JMSSerializer` serialize that field and you can set necessary type and other meta information on it.
Problem
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?