Matt Curry does a good job of providing a default model setup for any
new application. If AppController doesn't already exist, create /app/
app_controller.php and add the following class, or copy the attributes
into your current one:

    class AppController extends Controller {
        var $actsAs = array('Containable');
        var $recursive = -1;
    }

Doing so shouldn't require you to rework any of your code, but it will
reduce database overhead when dealing with hasMany and HABTM
relationships (provided you use the syntax of course). Then change
your find call to use Containable:

    $this->Member->contain(
        'County' => array(
            'Region' => array(
                'conditions' => array('Region.id' => $id)
                )
            )
        );
    $members = $this->Member->find('all');

I'm flying blind here, but hopefully it works. You can also pass the
contain settings to Model::find() each time, though it's too heavy for
complex joins:

    $members = $this->Member->find('all', array('contain' => array(
        'County' => array(
            'Region' => array(
                'conditions' => array('Region.id' => $id)
                )
            )
        )));

http://book.cakephp.org/view/474/Containable

On Jul 8, 10:17 am, "Dave Maharaj :: WidePixels.com"
<d...@widepixels.com> wrote:
>  Use "contain"
>
> There is a chapter in the cookbook how to get only the records from related
> models and not pull all the un-necessary info.
>
> Dave
>
> -----Original Message-----
> From: Alastair [mailto:m...@alastairmoore.com]
> Sent: July-07-09 10:31 PM
> To: CakePHP
> Subject: More HABTM questions - querying data
>
> Robert P kindly solved a problem I was having with updating records in a
> HABTM relationship so many thanks to him for that!
>
> I'm now trying to query the data but have added an additional model to my
> system, Regions. So it looks something like:
>
> Members HasAndBelongsToMany County
> County BelongsTo Region (and Region hasMany County)
>
> I'm trying to return members that belong to Region.id = 1 and am using this
> to do so:
>
> $members = $this->Member->County->Region->find('all', array ('conditions' =>
> array('Region.id' => $id), 'recursive' => 2));
>
> This does return all members belonging to Region 1 but it also returns a
> whole lot of other information with it. Is there any way I can construct a
> query that literally just returns the corresponding members?
>
> The above returns the following data:http://bin.cakephp.org/saved/48057
>
> which includes County and Region data and in this instance, I don't need it.
> I'll live with it if I need to but I'd rather I didn't have to! Removing
> 'recursive' => 2 results in just the Region and County data being returned.
>
> Many thanks,
>
> Alastair
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to 
cake-php+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to