php - Doctrine 1.2 calculating the sum of oneToMany relations with DQL

347

Using doctrine 1.2 I'm trying to do the following.:

lets say I have a model Job which has many Invoice(s).

In my invoice model I have a DQL event listener like

public function preDqlSelect(Doctrine_Event $event)
{
    $q = $event->getQuery();
    $q->addSelect('(hours * local_rate) as invoice_payout');
    $q->addSelect('(hours * global_rate) as invoice_bill');
    $q->addSelect('((hours * global_rate) - (hours * local_rate)) as invoice_profit');
}

which works fine, also to note the above is calculated as needed because depending on the user asking for it, it needs to be calculated differently.

The issue I am having is trying to do something like the following:

Select Job and foreach job->invoices SUM('invoice_payout) as job_payout, SUM('invoice_bill) as job_bill, SUM('invoice_profit') as job_profit.

I know the above is in no way correct syntax, but it was the best way I could explain what I was after.

177

Answer

Solution:

"invoice_payout" and "invoice_bill" do not physically exist in the table. You need to use the columns from the table. Try this:

public function preDqlSelect(Doctrine_Event $event)
{
    $q = $event->getQuery();
    $q->addSelect('SUM(hours * local_rate) as job_payout');
    $q->addSelect('SUM(hours * global_rate) as job_bill');
    $q->addSelect('SUM((hours * global_rate) - (hours * local_rate)) as job_profit');
    $q->groupBy('job_id');
}

People are also looking for solutions to the problem: php - Is there a way to get form requests from a html form post from the past?

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.