简体   繁体   English

在一个分页调用中为多个模型指定记录标准

[英]Specifying record criteria on more than one model in one pagination call

I am stuck on pagination in CakePHP 1.3. 我在CakePHP 1.3中停留在分页上。 I am trying to paginate feePayment records based on certain criteria. 我正在尝试根据某些标准对FeePayment记录进行分页。

feePayments belongs to Students which in turn belongs YearGroups. feePayments属于学生,而学生又属于YearGroups。

I want to paginate 'unpaid' feePayments for each year group. 我想对每个年度组的“未付”费用付款进行分页。 The problem I am having is that the SQL query seems to only take into account the conditions I specified for the FeePayment model and ignores the YearGroup criteria so only overdue unpaid records are returned regardless of the year group specified. 我遇到的问题是,SQL查询似乎仅考虑我为FeePayment模型指定的条件,而忽略YearGroup条件,因此无论指定的年份组如何,都仅返回过期的未付款记录。

Here is my code: 这是我的代码:

function unpaidClass($id) {
    $this->paginate = array(
    'FeePayment' => array ('recursive' => 1, 'conditions' => array('FeePayment.status' => 'Unpaid', 'FeePayment.due_date <= ' => date("Y-m-d"))),
    'YearGroup' => array ('recursive' => 1, 'conditions' => array('YearGroup.school_year' => $id))
    );

    $this->set('feePayments', $this->paginate());
}

Hope this makes sense, appreciate any help. 希望这有道理,感谢您的帮助。

Thanks, 谢谢,

Sid. 席德

You should consider using the Containable behavior . 您应该考虑使用Containable行为 This behavior allows you to group the necessary data you want without relaying on the "recursiveness" of your query. 此行为使您可以对所需的必要数据进行分组,而不必依赖查询的“递归性”。 You can place conditions on your contained data similar to the way you would in your queries and is a more permanent way to structure data across your application instead of specifying conditions over and over again in each query. 您可以像在查询中那样在包含的数据上放置条件,这是一种在整个应用程序中构造数据的更永久的方法,而不是在每个查询中一遍又一遍地指定条件。

Paginate should automatically pick up these associations when you Paginate your main model. 当您对主要模型进行分页时,分页应会自动选择这些关联。 Here's an example of what I mean here: http://cakephp.1045679.n5.nabble.com/Paginate-with-Containable-td1300971.html#a1300971 这是我的意思的示例: http : //cakephp.1045679.n5.nabble.com/Paginate-with-Containable-td1300971.html#a1300971

These should make your task easier. 这些应该使您的任务更容易。

After a lot of searching the net and reading CakePHP's documentation, here is the solution I came up with: 在大量搜索网络并阅读CakePHP的文档之后,这是我想出的解决方案:

    function unpaidClass($id) {
    $this->FeePayment->unbindModel(array(
        'belongsTo' => array('Student')
    ), $reset = 0);

    $this->FeePayment->bindModel(array(
        'belongsTo' => array(
            'Student' => array(
                'foreignKey' => false,
                'conditions' => array('Student.id = FeePayment.student_id')
            ),
            'YearGroup' => array(
                'foreignKey' => false,
                'conditions' => array('YearGroup.id = Student.year_group_id')
            )
        )
    ), $reset = 0);

    $this->paginate = array(
        'contain' => array('Student','YearGroup'),
        'conditions' => array('YearGroup.school_year' => $id, 
            'FeePayment.status' => 'Unpaid',
            'FeePayment.due_date <= ' => date("Y-m-d")));

    $this->set('feePayments', $this->paginate());
}

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

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