Hello

I'd be grateful for any advice on the best way to do this. (Cake 1.2
RC3)

I have a model Course, which BelongsTo School, which hasMany
Accommodation.
I want to be able to filter by courses, types of school and types of
accommodation.

The results also need to be paginated.

I've set up my model binding like this:
                $this->Course->bindModel(
                        array(
                                'belongsTo' => array(
                                        'School' => array(
                                                'className' => 'Schools.School',
                                                'foreignKey' => 'school_id'
                                        )
                                ),
                                'hasOne' => array(
                                        'Accommodation' => array(
                                                'className' => 
'Schools.Accommodation',
                                                'foreignKey' => false,
                                                'conditions' => 
array('Accommodation.school_id =
Course.school_id')
                                        )
                                )
                        ), false
                );

The Course hasOne Accommodation rather than the School hasMany
Accommodation to ensure that my accommodation results are returned and
can be searched on.

When I paginate I get a row for each accommodation as well as each
course, i.e.:

         [rows] => Array
                (
                    [0] => Array
                        (
                            [Course] => Array
                                (
                                    [id] => 44
                                    [name] => Test course
                                    [school_id] => 4
                                )

                            [School] => Array
                                (
                                    [id] => 4
                                    [name] => Test School
                                )

                            [Accommodation] => Array
                                (
                                    [id] => 6
                                    [school_id] => 4
                                    [name] => Test Accommodation
                                )

                        )

                    [1] => Array
                        (
                            [Course] => Array
                                (
                                    [id] => 44
                                    [name] => Test course
                                    [school_id] => 4

                                )

                            [School] => Array
                                (
                                    [id] => 4
                                    [name] => Test School
                                )

                            [Accommodation] => Array
                                (
                                    [id] => 7
                                    [school_id] => 4
                                    [name] => Test Accommodation 2
                                )

                        )
                                )

What I'm looking to get is results 'grouped' by course, while still
being able to filter by Accommodation field, e.g.:

                [rows] => Array
                        (
                                [0] => Array
                                        (
                                                [Course] => Array(...)
                                                [School] => Array(...)
                                                [Accommodation] => Array
                                                        (
                                                                [0] => 
Array(...)
                                                                [1] => 
Array(...)
                                                        )
                                        )
                        )

What's the best way to achieve this? Create a custom pagination query
and then somehow manipulate the resulting results array?

This is what I've added to my Course model so far:

function paginate($conditions, $fields, $order, $limit, $page = 1,
$recursive = null, $extra = array()) {
        $results = $this->find('all', array('conditions' => $conditions,
'fields' => $fields, 'order' => $order, 'limit' => $limit, 'page' =>
$page, 'recursive' => $recursive));
        // Would need to refactor the Array here
        return $results;
}

And the following paginateCount function that uses the group parameter
to only retrieve the correct number of results.

function paginateCount($conditions = null, $recursive = 0, $extra =
array()) {
        return $this->find('count', array('conditions' => $conditions,
'recursive' => $recursive, 'group' => 'Course.id'));
}

Can anyone advise me if I'm on the right track and the best way to
achieve the results I want? Is there a function in the Set class that
can do this?

Many thanks
Martin
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"CakePHP" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to