Sunday, August 23, 2015

how to use association with TableRegistry in cakephp 3

As you can see in 1st Business\Model\Entity\Business Object there is category association present, when called in BusinessController, But It's missing, when called in CategoriesController using TableRegistry. 
why that ? any idea how to get this fixed ? I called called below in BusinessesController

$this->autoRender = false;
        $this
->paginate = [
           
'contain' => ['Cities', 'Categories' ]
       
];
       
foreach( $this->paginate( $this->Businesses ) as $business ) {
            pr
( $business );
       
}

I got this results

Business\Model\Entity\Business Object
(
   
[id] => 1
   
[city_id] => 414
   
[postal_id] => 780299
   
[slug] => coatings-2000-lloydminster-ab-ca-1
   
[business_name] => Coatings 2000
   
[street_address] => 2B Production Ave
   
[address_locality] => Lloydminster
   
[address_region] => AB
   
[postal_code] => T9V 0Y4
   
[last_modified] => Cake\I18n\Time Object
       
(
           
[time] => 2015-03-12T00:00:00+0530
           
[timezone] => Asia/Calcutta
           
[fixedNowTime] =>
       
)


   
[status] => 1
   
[categories] => Array
       
(
           
[0] => Business\Model\Entity\Category Object
               
(
                   
[id] => 2276
                   
[parent_id] => 2212
                   
[category_slug] => protective-coatings
                   
[category_name] => Protective Coatings
                   
[lft] => 4449
                   
[rght] => 4450
                   
[level] => 2
                   
[_joinData] => Cake\ORM\Entity Object
                       
(
                           
[category_id] => 2276
                           
[business_id] => 1
                           
[[new]] =>
                           
[[accessible]] => Array
                               
(
                                   
[*] => 1
                               
)


                           
[[dirty]] => Array
                               
(
                               
)


                           
[[original]] => Array
                               
(
                               
)


                           
[[virtual]] => Array
                               
(
                               
)


                           
[[errors]] => Array
                               
(
                               
)


                           
[[repository]] => BusinessesCategories
                       
)


                   
[[new]] =>
                   
[[accessible]] => Array
                       
(
                           
[*] => 1
                       
)


                   
[[dirty]] => Array
                       
(
                       
)


                   
[[original]] => Array
                       
(
                       
)


                   
[[virtual]] => Array
                       
(
                       
)


                   
[[errors]] => Array
                       
(
                       
)


                   
[[repository]] => Business.Categories
               
)


       
)


   
[city] => Business\Model\Entity\City Object
       
(
           
[id] => 414
           
[city_slug] => lloydminster
           
[city_name] => Lloydminster
           
[[new]] =>
           
[[accessible]] => Array
               
(
                   
[*] => 1
               
)


           
[[dirty]] => Array
               
(
               
)


           
[[original]] => Array
               
(
               
)


           
[[virtual]] => Array
               
(
               
)


           
[[errors]] => Array
               
(
               
)


           
[[repository]] => Business.Cities
       
)


   
[[new]] =>
   
[[accessible]] => Array
       
(
           
[*] => 1
       
)


   
[[dirty]] => Array
       
(
       
)


   
[[original]] => Array
       
(
       
)


   
[[virtual]] => Array
       
(
       
)


   
[[errors]] => Array
       
(
       
)


   
[[repository]] => Business.Businesses
)

when i called this in categories controller, it shows missing category associations

$this->autoRender = false;
        $this
->paginate = [
           
'contain' => ['Cities', 'Categories' ]
       
];
        $businessTable
= \Cake\ORM\TableRegistry::get( 'Business.Businesses' );
       
foreach( $this->paginate( $businessTable ) as $business ) {
            pr
( $business );
       
}
I got this results with missing categories associations
Business\Model\Entity\Business Object
(
   
[id] => 1
   
[city_id] => 414
   
[postal_id] => 780299
   
[slug] => coatings-2000-lloydminster-ab-ca-1
   
[business_name] => Coatings 2000
   
[street_address] => 2B Production Ave
   
[address_locality] => Lloydminster
   
[address_region] => AB
   
[postal_code] => T9V 0Y4
   
[last_modified] => Cake\I18n\Time Object
       
(
           
[time] => 2015-03-12T00:00:00+0530
           
[timezone] => Asia/Calcutta
           
[fixedNowTime] =>
       
)


   
[status] => 1
   
[categories] => Array
       
(
       
)


   
[city] => Business\Model\Entity\City Object
       
(
           
[id] => 1
           
[city_slug] => albany
           
[city_name] => Albany
           
[[new]] =>
           
[[accessible]] => Array
               
(
                   
[*] => 1
               
)


           
[[dirty]] => Array
               
(
               
)


           
[[original]] => Array
               
(
               
)


           
[[virtual]] => Array
               
(
               
)


           
[[errors]] => Array
               
(
               
)


           
[[repository]] => Business.Cities
       
)


   
[[new]] =>
   
[[accessible]] => Array
       
(
           
[*] => 1
       
)


   
[[dirty]] => Array
       
(
       
)


   
[[original]] => Array
       
(
       
)


   
[[virtual]] => Array
       
(
       
)


   
[[errors]] => Array
       
(
       
)


   
[[repository]] => Business.Businesses
)

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