I did something similar using the relations of the main model in this
instance.  In this case the model 'Message' could be associated with
different things, 'Product', 'Invoice', etc.  There were only a few, so I
made a separate relation for each using the 'conditions' parameter to filter
by related model name.

Example belongsTo for model 'Message':
var $belongsTo = array(
'Invoice' => array(
'className' => 'Invoice',
'foreignKey' => 'reference_id',
'conditions' => array('Message.reference_model' => 'Invoice'),
'fields' => '',
'order' => ''
),
'Product' => array(
'className' => 'Product',
'foreignKey' => 'reference_id',
'conditions' => array('Message.reference_model' => 'Product'),
'fields' => '',
'order' => ''
)
);

Jeff

On Wed, Feb 3, 2010 at 5:30 AM, Jon Bennett <[email protected]> wrote:

> Hi AD,
>
> >> I think what I want is a habtm that can connect to multiple models
> >> with some additional fields in the join table, but I'm not sure that's
> >> feasible, but hope it is.
> >
> > a join table with more than 2 fields is a model, and a model that can
> > link to anything is (as you know) polymorphic.
> >
> > Therefore you want a join table something like this:
> >
> > relations
> >  id
> >  model
> >  foreign_id
> >  url
> >  title
> >  related_model
> >  related_foreign_id
> >  related_url
> >  related_title
> >
> > which would allow you to relate anything to anything. I wouldn't use
> > the polymorphic behavior though. just create a method for returning
> > you related stuff - unless you denormalize as hinted above so you
> > don't even need to.
> >
> > class Relation extends AppModel {
> >  function related($model, $id) {
> >  $return = array();
> >  $data = $this->find('all', array('conditions' => array(
> >   'OR' => array(
> >     array('model' => $model, 'foreign_id' => $id),
> >     array('related_model' => $model, 'related_foreign_id' => $id),
> >   )
> >  );
> >  }
> >  .. efficiently loop and get data if at all necessary
> >  return $return;
> > }
>
> Ahh, ok - thanks. Will have a play then hit you back if I come unstuck.
>
> Cheers,
>
> jb
>
>
> --
> jon bennett - www.jben.net - blog.jben.net
>
> Check out the new CakePHP Questions site http://cakeqs.org and help others
> with their CakePHP related questions.
>
> 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]<cake-php%[email protected]>For
>  more options, visit this group at
> http://groups.google.com/group/cake-php?hl=en
>

Check out the new CakePHP Questions site http://cakeqs.org and help others with 
their CakePHP related questions.

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