With your associations setup, you shouldn't need to add another one to directly link Location with Photoset - if you do a Location::find with recursive set to 2 then you'll get all the associated Photoset-s, albeit tree-structured in the Event data.
Since the data is already there, there is little point in tacking on another query to get the same info - if you really really need it in a separate array then you should extract the info in Location::afterFind() - a simple loop. Regarding the other way up, recursive belongsTo is not currently implemented - https://trac.cakephp.org/ticket/1336, though I hope this gets sorted soon. In the meantime, you CAN use finderQuery on belongsTo, just make sure you set the external attribute: class Location { // ... var $belongsTo = array( 'Photoset' => array( 'external' => true, 'finderQuery' => 'SELECT Photoset.* FROM photosets AS Photoset, events AS Event WHERE Photoset.event_id=Event.id AND Event.location_id={$__cakeID__$}' ) ); // ... } (This is for Cake 1.2 - I've no idea about 1.1) This will cost you an extra query per row though, so it's slower than a "normal" association. Personally I'd like to have the option of setting the foreignKey attribute in belongsTo/hasOne associations to FALSE and rely on the conditions to do the rest - it would make bits like this much easier. Hope this helps! On Sep 26, 12:31 am, Tim Molendijk <[EMAIL PROTECTED]> wrote: > Hello, > > I'm trying to create these associations that are not really (directly) > there on the database level, but should be present in my CakePHP > model. To give an idea of what I'm talking about, consider the > following example. > > Assume three different models: > - Location > - Event > - Photoset > > With associations as follows: > - Location hasMany Event (and Event belongsTo Location) > - Event hasMany Photoset (and Photoset belongsTo Event) > > So on the database level there is no direct reference between Location > and Photoset and vice versa. Now I want to add a 'virtual' association > to my CakePHP model definition: > - Location hasMany Photoset (and Photoset belongsTo Location). > > The logic behind this association should be clear: all photosets that > belong to any event that belongs to a location, should belong to > location. I have been trying to create this association by defining a > custom hasMany['Photoset'] on Location through a custom query in its > 'finderQuery' attribute. This worked fine. > > But the other way around I cannot think of a way to define the > corresponding belongsTo['Location'] on Photoset. I thought of two > options: > - defining the Photoset->belongsTo['Location'] through a custom query. > This is not possible since belongsTo does not provide a 'finderQuery' > attribute. > - setting the 'foreignKey' attribute of Photoset->belongsTo['Location'] in > such a manner that it would refer to > > Photoset->data['Event']['location_id']. This did not work either since > 'foreignKey' does not seem to accept anything more complex than the > name of the foreign key field in the database table of Photoset. > > Does anyone have some more ideas? Or am I trying to do something that > I shouldn't want to do? > > Thanks a lot, > > Tim --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Cake PHP" 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 -~----------~----~----~----~------~----~------~--~---
