nicolallias
nicolallias

Reputation: 1122

Doctrine subtract on-the-fly two columns as a third

Expressed in MySQL, what I wish:

SELECT article.sellprice - article.cost AS margin
FROM article

My attempt in DQL (note that margin does not exist in my entity):

SELECT a, a.sellprice - a.cost AS a.margin
FROM ArticleBundle:Article a"

Results in an exception [Syntax Error] line 0, col 47: Error: Expected Doctrine\ORM\Query\Lexer::T_FROM, got '.'. Attempt to get it outside of the entity:

SELECT a, a.sellprice - a.cost AS margin
FROM ArticleBundle:Article a"

And some variations of that code; that messes up with my result, causing a twig error Key "nom" for array with keys "0, margin" does not exist.. In fact, a.nom exists; when reverting to:

SELECT a
FROM ArticleBundle:Article a

No error at all. Any clues to get my calculated on-the-fly result?

Upvotes: 0

Views: 622

Answers (1)

nicolallias
nicolallias

Reputation: 1122

Because that value must bubble up to the twig, according to the comment of Yoshi (many thanks by the way), I ended up with the second try:

SELECT a, a.sellprice - a.cost AS margin
FROM ArticleBundle:Article a"

Tuning the twig to parse that mixed content:

{% for mixed_content in pagination %}
    <tr>
    {% set article = mixed_content|first %}
    {% set margin = mixed_content.margin %}
        <td>{{ article.nom }}</td>
        <td>{{ article.margin}}</td>

Hoping that may help anyone.

Upvotes: 1

Related Questions