简体   繁体   English

Doctrine 1.2 用 DQL 计算 oneToMany 关系的总和

[英]Doctrine 1.2 calculating the sum of oneToMany relations with DQL

Using doctrine 1.2 I'm trying to do the following.:使用 doctrine 1.2 我正在尝试执行以下操作:

lets say I have a model Job which has many Invoice(s).假设我有一个 model 工作,它有很多发票。

In my invoice model I have a DQL event listener like在我的发票 model 中,我有一个 DQL 事件侦听器,例如

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. Select Job 和 foreach job->invoices SUM('invoice_payout) 作为 job_payout,SUM('invoice_bill) 作为 job_bill,SUM('invoice_profit') 作为 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.我知道上面的语法绝不是正确的,但这是我可以解释我所追求的最好的方式。

"invoice_payout" and "invoice_bill" do not physically exist in the table. “invoice_payout”和“invoice_bill”实际上并不存在于表中。 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');
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM