If you're going to need the direct association between Location and
Photoset a lot, it might
well be worth having an extra location_id field on the photosets
table, and really making the
association direct. Sure, it will mean that your database won't be in
NF 2 (or is it 3?), but it will be
worth the trouble. Keeping them in sync should be quite easy :
class Photoset extends AppModel {
function beforeSave() {
// Never change the location_id directly from Photoset
unset($this->data['Photoset']['location_id']);
// If the event_id might be changing, sync the location_id on the
event's
if (!empty($this->data['Photoset']['event_id'])) {
$this->data['Photoset']['location_id'] = $this->Event-
>field('location_id', array('id' => $this->data['Photoset']
['event_id']));
}
return true;
}
}
class Event extends AppModel {
function afterSave() {
if (!empty($this->data['Event']['id']) && !empty($this-
>data['Event']['location_id'])) {
// No Model::updateAll() on cake 1.1.x, so do it the old-
fashioned way
$this->execute('UPDATE photosets SET location_id=" . $this-
>data['Event']['location_id'] . " WHERE event_id=" . $this-
>data['Event']['id']);
}
}
}
This might not cover all eventualities (I haven't tested it), but the
theory is sound.
It might be a tad more difficult to sync updates this way, but it will
make all finds much faster.
On Sep 26, 12:25 pm, Tim Molendijk <[EMAIL PROTECTED]> wrote:
> Thanks for your reply.
>
> I agree that in theory it's not necessary to directly associate
> Location with Photoset. But in reality I think it can be really
> useful. For example, when you have *a lot* of events associated to a
> Location, but only a couple of them have (one or a few) photosets.
> Then imagine I'm implementing an action on the location controller
> which doesn't need events, but does need photosets. What I do in
> situations like these is unbind the hasMany Event association. Without
> an explicit association from Location to Photoset this would make me
> lose the photosets as well (regardless the recursive depth that I
> apply).
>
> All in all, I've concluded that a 'virtual' association is not really
> something you should want with CakePHP 1.1.x, and I will request the
> photosets that belong to a location manually in every action that
> desires them, using a custom method on the location model. It's not as
> neat as the virtual association idea, but I think it's still a decent
> solution.
>
> Tim
>
> On Sep 26, 11:30 am, grigri <[EMAIL PROTECTED]> wrote:
>
> > 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
-~----------~----~----~----~------~----~------~--~---