Thursday, January 8, 2015

Re: belongsTo custom condition not working

As the one reply you got on stackoverflow states - you should use the HABTM relationship.

Assuming you have the base tables as:
people
organsations

To have one person follow another person just means that there must be a relationship between one person and another person in the people table. As following usually means that one person can be followed by many others and one person can follow many others, then you need to use/define the HABTM relationship in your models.

The CakePHP book (2.x) describes the relationships here:
http://book.cakephp.org/2.0/en/models/associations-linking-models-together.html#

In the models that would be something like this:
Model Person HABTM Person - that is two relationship with itself, one for follower and one for followed. See the CakePHP book here:
http://book.cakephp.org/2.0/en/models/associations-linking-models-together.html#multiple-relations-to-the-same-model
Which describes how to define more than one relationships to the same model.

Example:
Person HABTM Follower with (className => Person) - this means that the Follower model in real life is the Person model.

Tablewise that means that you should have the following tables:
people
followers_people (need to be alphabetically correct) - in case you already has such a table for a different purpose, then name it differently and just tell your model about the name.

Between people and organisation it should be easier, as they are different tables and thus different models.

Try it out and keep us informed of the result :)
Enjoy, John
Just need the


On Thursday, 8 January 2015 01:35:14 UTC+2, Matt Myers wrote:
John,

I have another similar challenge I'm working on and I thought you may be able to help.


Any help would be appreciated. Thanks.

Matt

-- 
Matt Myers
Sent with Sparrow

On Thursday, October 16, 2014 at 9:41 AM, Matt Myers wrote:

Thanks John! This looks great. I'll give it a try.

--
Matt


On Wednesday, October 15, 2014 12:35:40 PM UTC-6, John Andersen wrote:
Hi Matt

Created a test setup and got the result you wanted by defining each model as follows:

class LinkedinPerson extends AppModel {
    public $hasMany = array(
        'LinkedinRecommendation' => array(
            'className' => 'LinkedinRecommendation',
            'foreignKey' => 'linkedin_id',
        )
    );
    public $primaryKey = 'linkedin_id';
    public $actsAs = array('Containable');


class LinkedinRecommendation extends AppModel {
    public $hasMany = array(
        'LinkedinPerson' => array(
            'className' => 'LinkedinPerson',
        )
    );
    public $actsAs = array('Containable');


In the LinkedinPeople controller I have the index function retrieve all the LinkedinPeople as:
    public function index() {
        $this->set('people', $this->LinkedinPerson->find('all', array(
            'contain' => array('LinkedinRecommendation')
        )));
    }

This gives me the following result (debug output):
Array  (      [0] => Array          (              [LinkedinPerson] => Array                  (                      [id] => 1                      [name] => test person1                      [linkedin_id] => 11111111                  )              [LinkedinRecommendation] => Array                  (                      [0] => Array                          (                              [id] => 1                              [content] => Recommendation 1                              [linkedin_id] => 11111111                              [by_linkedin_id] => 44444444                          )                      [1] => Array                          (                              [id] => 2                              [content] => Recommendation 2                              [linkedin_id] => 11111111                              [by_linkedin_id] => 22222222                          )                      [2] => Array                          (                              [id] => 3                              [content] => Recommendation 3                              [linkedin_id] => 11111111                              [by_linkedin_id] => 22222222                          )                  )          )      [1] => Array          (              [LinkedinPerson] => Array                  (                      [id] => 2                      [name] => test person2                      [linkedin_id] => 22222222                  )              [LinkedinRecommendation] => Array                  (                      [0] => Array                          (                              [id] => 4                              [content] => Recommendation 4                              [linkedin_id] => 22222222                              [by_linkedin_id] => 33333333                          )                      [1] => Array                          (                              [id] => 7                              [content] => Recommendation 7                              [linkedin_id] => 22222222                              [by_linkedin_id] => 11111111                          )                  )          )      [2] => Array          (              [LinkedinPerson] => Array                  (                      [id] => 3                      [name] => test person3                      [linkedin_id] => 33333333                  )              [LinkedinRecommendation] => Array                  (                      [0] => Array                          (                              [id] => 5                              [content] => Recommendation 5                              [linkedin_id] => 33333333                              [by_linkedin_id] => 11111111                          )                      [1] => Array                          (                              [id] => 6                              [content] => Recommendation 6                              [linkedin_id] => 33333333                              [by_linkedin_id] => 33333333                          )                  )          )      [3] => Array          (              [LinkedinPerson] => Array                  (                      [id] => 4                      [name] => test person4                      [linkedin_id] => 44444444                  )              [LinkedinRecommendation] => Array                  (                      [0] => Array                          (                              [id] => 8                              [content] => Recommendation 8                              [linkedin_id] => 44444444                              [by_linkedin_id] => 11111111                          )                  )          )  )

Hope you can use the above. Please note that I always uses the Containable behavior.
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+u...@googlegroups.com.
To post to this group, send email to cake...@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: