Вопрос

I'm testing Has And Belongs To Many relations in cakePHP. Here is my database Model :

Contact Has And Belongs To Many Contact

Contact has one Person

Thus, my contact model looks like this :

public $hasOne = array(
    'Person'
);
public $hasAndBelongsToMany = array(
    'ContactLinks' => array(
        'className' => 'Contact',
        'joinTable' => 'contacts_contacts',
        'foreignKey' => 'contact_id_1',
        'associationForeignKey' => 'contact_id_2',
    )
);

To avoid using a deep recursive value, I use containable bahavior. Here is my find :

$data = $this->Contact->find('first', array(
                        'conditions' => array(
                                'Contact.id' => 15
                        ),
                        'contains' => array(
                            'Contact' => array(
                                'Person'
                            ),
                        ),
                    ));

This request looks fine to me. As Contact hasOne Person, I look for Person into Contact container. However, it does not work :(

And my result array looks like this :

[ContactLinks] => Array
    (
        [0] => Array
            (
                [id] => 56
                [created] => 0000-00-00 00:00:00
                [modified] => 2013-10-31 11:41:28
                [creator_id] => 1
                [modificator_id] => 2
                [type] => person
                [ContactsContact] => Array
                    (
                        [id] => 1
                        [contact_id_1] => 15
                        [contact_id_2] => 56
                        [link_type] => firm
                        [link_description] => Stagiaire
                    )

            )

        [1] => Array
            (
                [id] => 30
                [created] => 0000-00-00 00:00:00
                [modified] => 0000-00-00 00:00:00
                [creator_id] => 1
                [modificator_id] => 
                [type] => person
                [ContactsContact] => Array
                    (
                        [id] => 2
                        [contact_id_1] => 15
                        [contact_id_2] => 30
                        [link_type] => 
                        [link_description] => Patron
                    )

            )

    )

It seems like CakePHP doesn't make the link between Contact and Person.

Any idea about how to solve this ?

Thanks !

Это было полезно?

Решение

I think the problem comes when using the wrong alias name. Try this

$data = $this->Contact->find('first', array(
  'conditions' => array(
  'Contact.id' => 15
 ),
'contains' => array(
 'ContactLinks' => array('Person'))
));

EDIT: Typo alert -> 'contains' should be 'contain'

$data = $this->Contact->find('first', array(
  'conditions' => array(
  'Contact.id' => 15
 ),
'contain' => array(
 'ContactLinks' => array('Person'))
));
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top