Wednesday, January 7, 2015

Re: belongsTo custom condition not working

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+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: