Thanks for the quick reply.
I tried what you suggested but i got the same result by writting the
Profile model like this:

class Profile extends AppModel
{
        var $name = 'Profile';

        var $recursive = 3;
        var $belongsTo = array('Location' =>
                                                           array('className'  
=> 'Country',
                                                                         
'conditions' => '',
                                                                         
'order'          => '',
                                                                         
'foreignKey' => 'location_id'
                                                           ),
                                                   'Nationality' =>
                                                           array('className'  
=> 'Country',
                                                                         
'conditions' => '',
                                                                         
'order'          => '',
                                                                         
'foreignKey' => 'nationality_id'
                                                           )
                                                 );
}

The problem is that when i do a findAll on the Users, it first runs a
query that selects Users with left joined Profiles, then it runs a
query for each Profile, and then one query for each Country in each
Profile.
So to select 2 Users it uses 5 queries.
I would like to have one big query... or at least avoid the query that
selectes the Profile since that is already selected in the Users query
(with joins)

On Mar 7, 8:43 am, Langdon Stevenson <[EMAIL PROTECTED]> wrote:
> Hi gribelu
>
> What you need to do is create two new models for the countries table:
>
>    location.php
>    nationality.php
>
> These models will both use the countries table, and can, if you choose
> have a hasMany relationship to Profile.
>
> Then in your Profile model, you have two belongsTo relationships, one to
> each of the new models above.  In the Profiles table you need to rename
> your foreign key fields:
>
>    location_id
>    nationality_id
>
> This should do the trick, and Cake will do all of the heavy lifting for
> you.  No queries to write.
>
> I recommend that you read up in the manual about belongsTo relationships
> and using tables with different names in a model.  That should fill in
> the details that I have not included here.
>
> Regards,
> Langdon
>
> > I'm currently building the front page for an application. On this page
> > there's a list of users with some of their profile data.
>
> > The model looks kinda like this: "User hasOne Profile"
>
> > In the 'profiles' table i have two fields 'location' and 'nationality'
> > wich both point to id's from the "countries" table. What i want is to
> > get the country_name from that table and use it in my view.
> > Using multiple "Profile hasOne Country" doesn't work (duh).. If i use
> > multiple "Country belongsTo Profile" almost works but it doesn't use
> > joins and it's just plain wrong for what i need.
>
> > Is there a way to make this work without writting the entire query by
> > hand? Also, i don't want multiple queries per user.. that would be bad
> > for performance.
>
> > Thank you


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Cake 
PHP" group.
To post to this group, send email to cake-php@googlegroups.com
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