简体   繁体   English

在Yii中处理关系ActiveRecord的最佳方法

[英]Best approach handling Relational ActiveRecords in Yii

I'm quite confused with the different ways of "acquiring" data from my database using ActiveRecords. 我对使用ActiveRecords从数据库“获取”数据的不同方式感到困惑。 For this specific problem I don't know wether I should use model->findAll(), criteria (which would allow using pagination), or plain SQL if necessary (given that I need to display the output in a GridView). 对于这个特定的问题,我不知道是否应该使用model-> findAll(),条件(允许使用分页)或必要时使用普通SQL(假设我需要在GridView中显示输出)。

What I'm trying to do here is list all the companies in my table 'company' and how much money has each of them payed. 我要在这里做的是列出表“ company”中的所有公司以及它们各自支付了多少钱。 Payments are registered in the table 'transaction', which has the field 'value' representing the amount of money. 付款记录在“交易”表中,该表的“值”字段代表金额。

So, if I had to use a SQL query, it would be something like: 因此,如果我必须使用SQL查询,它将类似于:

SELECT * 
FROM company 
LEFT OUTER JOIN transaction 
    ON company.id = transaction.company 
GROUP BY value

In short, what is the easiest way to achieve this? 简而言之,最简单的方法是什么? Thank you. 谢谢。

code for dataprovider 数据提供者代码

$dataProvider=new CActiveDataProvider('Company', array(
    'criteria'=>array(
        'with'=>array('transaction'),
        'group' = 't.value'
    ),
    'pagination'=>array(
        'pageSize'=>20,
    ),
));

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

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