cakephpcontainable

Cakephp Containable not working at all


I have been banging my head on the wall over this. I have a model Sku that belongs to model Purchase. My AppModel has $actAs=array('Containable') and $recursive=-1

Inside SkuController, when I do $this->Sku->find('all', array('contain' => 'Purchase')); I don't get Purchase. I have searched many old questions here and elsewhere on Internet but just can't seem to resolve this. To check if Containable behavior is being loaded, I edited ContainableBehavior.php in lib\Cake\Model\Behavior to make it an invalid php file but that didn't produce any errors. What the heck is wrong!!

Here's the SQL from debug:

SELECT Sku.id, Sku.purchase_id, Sku.item_id, Sku.upc, Sku.quantity_avail, Sku.per_unit_price_amt, Sku.do_not_delete, Sku.created, Sku.modified, (concat('SK',lpad(Sku.id,8,'0'))) AS Sku__idFormatted FROM sellble.skus AS Sku WHERE 1 = 1 ORDER BY Sku.id desc

CakePHP ver: 2.4.4


Solution

  • Not sure if this is different across versions but I have always specified the contain within an array and that works fine for me.

    $this->Sku->find('all', array('contain' => array('Purchase')));
    

    Or for mapping only the fields or conditions you want:

    $this->Sku->find('all',
         array('contain' => array(
             'Purchase' => array(
                 'fields' => Purchase.name
                 'conditions' => array(
                      Purchase.name = 'somename'
                      )
                 )
             )
         )
    );