Tuesday, April 29, 2014

Cakephp and HasMany not returning association

I have started to play around with 3.0.  I have taken one of our models from 2.x and moving to 3.0 for practice and testing.     I have come across and issue when calling a Model/Table and attempting to include an hasMany association.   But, even though I use contain in the call it does add the association to the sql statement.    Here is the breakdown (superfluous code removed).  Using 3.0 code current as of 4/29/2014.  Is there something I am missing/    // Products which has many association of images:  class ProductsTable extends Table {        public function initialize(array $config) {          $this->table('products');          $this->displayField('name');          $this->primaryKey(['id']);          $this->addBehavior('Timestamp');            $this->hasMany('ProductImages', [                  'foreignKey' => 'product_id',                  //            'joinType' => 'LEFT'              ]);      }  }  // Product Images belongs to Products  class ProductImagesTable extends Table {      public function initialize(array $config)      {          $this->table('product_images');          $this->displayField('name');          $this->primaryKey(['id']);          $this->addBehavior('Timestamp');            $this->belongsTo(              'Products',              [                  'foreignKey' => 'product_id',                  //            'joinType' => 'LEFT'              ]          );      }  }    class ProductsController extends AppController {      public function index()      {          // calling $this->Products gives the same results          $p = TableRegistry::get('Products');          $pQuery = $p              ->find()              ->where(['status' => 'active'])              ->select(['id', 'name', 'price'])              ->order(['Product.name' => 'ASC'])              /* ====	 tried just calling  ->contain(['ProductImages')	=====  not working either */              ->contain(                  [                      'ProductImages' => function ($q) {                          return $q                              ->select(['id', 'product_id', 'name'])                              ->order(['ProductImages.position' => 'ASC']);                      }                  ]              );            pr($pQuery->sql());          // This shows the incorrect sql without the contain association          // SELECT Products.id AS `Products__id`, Products.name AS `Products__name`, Products.price AS `Products__price`          //    FROM products AS Products WHERE status = :c0 ORDER BY Product.name ASC            $pi = TableRegistry::get('ProductImages');          $piQuery = $pi              ->find('all')              ->select(['name', 'id', 'product_id'])              ->contain(['Products']);          pr($piQuery->sql());          // This shows the correct sql          // SELECT ProductImages.name AS `ProductImages__name`, ProductImages.id AS `ProductImages__id`,          // ProductImages.product_id AS `ProductImages__product_id`          // FROM product_images AS ProductImages          // LEFT JOIN products Products ON Products.id = (ProductImages.product_id)            exit;      }  }    

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