views:

33

answers:

2

Hi all,

I am stuck on pagination in CakePHP 1.3. I am trying to paginate feePayment records based on certain criteria.

feePayments belongs to Students which in turn belongs 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.

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.

A: 

You should consider using the Containable behavior. 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

These should make your task easier.

Michael
This looks interesting. I am giving it a go. But I'm not sure if this is what I really need because I want to paginate data from just one model. I just need the other models to search through the associated records. Most of the pagination questions I have come accross whilst searching for an answer to my problem involve the user wanting to paginate data from associated models.
Sid
I tried to use contain but its giving me the following error: Unknown column 'YearGroup.school_year' in 'where clause' Here is my code: function unpaidClass($id) { $this->paginate['FeePayment'] = array( 'contain' => array('Student', 'YearGroup'), 'conditions' => array('FeePayment.status' => 'Unpaid', 'FeePayment.due_date <= ' => date("Y-m-d"), 'YearGroup.school_year' => $id) ); $this->set('feePayments', $this->paginate('FeePayment')); }I am totally stumped...
Sid
You don't put your conditions in the same format as you do with your find() calls. Going with the example I linked (http://cakephp.1045679.n5.nabble.com/Paginate-with-Containable-td1300971.html#a1300971), your code might look like this: http://pastebin.com/snPi4kQzNote my comments for guidance.
Michael
Also, use the 'fields' option carefully. You also need to include all the fields used in your association ids for that model otherwise you'll lose those associations in your returned data.
Michael
Thanks for your help so far Placer14, appreciate it. Unfortunately, I still can't get the right records to paginate. All the records are paginated. The SQL statement seems to ignore the fee_payment field conditions. In addition, a error is displayed along the top of the page: "Warning (512): Model "FeePayment" is not associated with model "FeePayment" [CORE/cake/libs/model/behaviors/containable.php, line 363]"I have come up with the following code: http://pastebin.com/5XS0QwNBOnce again, thanks for your help dude.
Sid
After searching around on the net, I don't think I'm meant to include the FeePayment model within the contain array because I am calling the paginate method from the FeePayment object anyway..but then that brings up the issue of where to specify the conditions for the FeePayment fields.. :S
Sid
If you're still having trouble on this, you can stop in on the #cakephp channel on freenode.net. Plenty of cake devs are around to help.
Michael
A: 

After a lot of searching the net and reading CakePHP's documentation, here is the solution I came up with:

    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());
}
Sid