Thursday, January 8, 2015

Re: belongsTo custom condition not working

John,

Thanks for your help.

Yes, the containable behavior is critical. I use it all over.

I have 'persons' and 'organizations' tables. Here's the outcome I'm trying to get:

$following = $this->Person->find('all', array(
    'contain' => array('Following' => array('FollowPerson','FollowOrganization')),
    'conditions' => array('Person.id' => 1234),
));

$following will return all the Person and Organization records that are following Person 1234.

$followers = $this->Person->find('all', array(
    'contain' => array('Follower' => array('Person')),
    'conditions' => array('Person.id' => 1234),
));

$followers will return all the Person records that Person 1234 is following.

Same for Organization:

$followers = $this->Organization->find('all', array(
    'contain' => array('Follower' => array('Person')),
    'conditions' => array('Organization.id' => 5678),
));

The $followers logic currently works correctly. But the $following logic is very close, except that it returns all records matching the table_id, not filtering on the table_name.

Since is is always a person that is following something else (Person, Organization, and more in future), I have:

Person hasMany PersonFollow (tableName='person_follows'; realize not alphabetical).
PersonFollow belongsTo Person

The challenging part from here is PersonFollow HABTM Persons and Organizations.

I tried this in the PersonFollow model:

public $hasAndBelongsToMany = array(
    'FollowPerson' => array(
        'className' => 'Person',
        'joinTable' => 'person_follows',
        'foreignKey' => 'table_id',
        'associationForeignKey' => 'id',
        'conditions' => array('PersonFollow.table_name' => 'persons'),
    ),
    'FollowOrganization' => array(
        'className' => 'Organization',
        'joinTable' => 'person_follows',
        'foreignKey' => 'table_id',
        'associationForeignKey' => 'id',
        'conditions' => array('PersonFollow.table_name' => 'organizations'),
    ),
);

Seems to make no difference.

Should I be taking an entirely different approach here?

-- 
Matt Myers
Sent with Sparrow

On Thursday, January 8, 2015 at 12:17 PM, John Andersen wrote:

Just an addition to my previous post

Take a look at the Containable behaviour in the CakePHP book here:
http://book.cakephp.org/2.0/en/core-libraries/behaviors/containable.html

Understand it and then use it - it will make your life so much simpler when you later need to retrieve data.
Enjoy, John

--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP

---
You received this message because you are subscribed to a topic in the Google Groups "CakePHP" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/cake-php/MAW94cc-jeQ/unsubscribe.
To unsubscribe from this group and all its topics, send an email to cake-php+unsubscribe@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/d/optout.

--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP

---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscribe@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/d/optout.

No comments: