referential hasMany Through associations
I'm trying to get my head around bi-directional self-referential hasMany through relationships in CakePHP (what a mouthful!).
I'm working on a picture matching website.
I've started by defining a hasMany through relationship with a join model.
The pictures_matches join table has this structure:
id | picture_id | partner_id | rating | total_votes
My match join model association looks like this:
class PictureMatch extends AppModel {
...
public $belongsTo = array(
'Picture' => array(
'className' => 'Picture',
'foreignKey' => 'picture_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
'Partner' => array(
'className' => 'Picture',
'foreignKey' => 'partner_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
);
}
Each picture needs to be able to access its related pictures from either direction, but this is where my grasp is slipping. It looks like I need to save both sides of the relationship but this destroys the extra data stored in the join model - with two db entries, voting could vary depending on the direction.
Can anyone shed any light on the best way to do this in CakePHP? I'm rather confused.
Is it possible to create the inverse relationships on the fly?
You can create realtions on the fly vie Model::bindModel(), very usefull stuff this would alow you to bind reverse relations or rather any direction you would like on the fly.
http://book.cakephp.org/2.0/en/models/associations-linking-models-together.html
Also using Containable behaviour you can create infinite chain of retriving your associated date ex.
contain('Picture.PictureMatch.Partner.PictureMatch.Picture.....')
Basically you can loop through all of your models as long as each chain is somehow related to the next one to explain it better simple example ( please disregard logic in it )
Circle belongsTo Square Square belongsTo Triangle
So Triangle is not related to Circle ( directly ), but Square is kinda in between
Circle->find('all', array('...', contain => array('Square.Triangle'));
or to have more fun lets get circle by circle with loop around
Circle->find('all', array('...', contain => array('Square.Trinagle.Square.Circle'));
and so on, of course those example are useless and without any programming logic, but I hope you understand the point that you can loop trough infinite number of relations going back and forth.
I am not sure if this is the best solution but if you did this:
public $belongsTo = array(
'Picture1' => array(
'className' => 'Picture',
'foreignKey' => 'picture_id',
),
'Picture2' => array(
'className' => 'Picture',
'foreignKey' => 'picture_id',
),
'Partner' => array(
'className' => 'Partner',
'foreignKey' => 'partner_id',
),
);
then when you do a search you just search for ($this->data['Picture1'] == $var || $this->data['Picture2'] == $var)
and so long as you have recursive
set to 1 or 2 you should get back all the related data for that Picture
.
I assume this is abandoned, but it's easily resolvable -- it has to do with the phase
destroys the extra data stored in the join model
That means your saves are running a deleteAll
and inserting the record on matching... instead you need to find and update that record...
This can be done in a few ways, but the easiest is before your save call, look it up, and include the primary key in the match record data. Basically don't save it as a HABTM, and only save it as a hasMany if you've already tried to find an existing match record primary key ( id
) and updated the data to save with it.
上一篇: HABTM with self需要2x连接表中的行吗?
下一篇: 参照hasMany通过协会