CakePHP: bidirectional self-referencing hasMany Through Associations

I'm trying to figure out bidirectional self-referencing hasMany through relationships in CakePHP (what a sip!).

I am working on a site that matches the picture.

  • Images are linked to other images through a “match” (association model).
  • Each match has two photos and saves the current rating and the total number of votes.
  • When viewing an image, all images associated with it from any direction should be accessible (through its coincidence).

I started by defining a hasMany relationship with the connection model.

The join_matches join table has this structure:

id | picture_id | partner_id | rating | total_votes

My union link 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' => ''
        )
    );
}

, , . , , , - , .

- CakePHP? . " "?

+5
3

vie Model:: bindModel(), , , , , .

http://book.cakephp.org/2.0/en/models/associations-linking-models-together.html

, Containable behavior, .

( 'Picture.PictureMatch.Partner.PictureMatch.Picture.....')

, , - , (, )

, (),

Circle->find('all', array('...', contain => array('Square.Triangle')); 

,

Circle->find('all', array('...', contain => array('Square.Trinagle.Square.Circle'));

.., , , , , , .

0

, , :

public $belongsTo = array(
    'Picture1' => array(
        'className' => 'Picture',
        'foreignKey' => 'picture_id',
    ),
    'Picture2' => array(
        'className' => 'Picture',
        'foreignKey' => 'picture_id',
    ),
    'Partner' => array(
        'className' => 'Partner',
        'foreignKey' => 'partner_id',
    ),
);

, , ($this->data['Picture1'] == $var || $this->data['Picture2'] == $var), recursive, 1 2, Picture.

0

, , -

,

, deleteAll ... ...

This can be done in several ways, but the simplest of them is to call the save, look at it and include the primary key in the data of the correspondence record. Basically, do not save it as HABTM and only save it as hasMany if you have already tried to find the existing primary key of the match record ( id) and updated the data to save with it.

0
source

All Articles