0

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

vikc
  • 65
  • 1
  • 7
  • Is Sku model associated with Purchase? – Guillermo Mansilla Feb 05 '14 at 23:46
  • Are you using `actAs` or the proper `actsAs` variable? Are your variable assignments public, i.e. `public $actsAs = array('Containable');`? If you remove the recursive setting, does the purchase data return in your find (just to check the association)? If you create a reflection of your model, does it carry the array containing 'Containable' in the actsAs variable? – Scott Harwell Feb 06 '14 at 01:31

1 Answers1

1

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'
                  )
             )
         )
     )
);
JamesLee
  • 155
  • 7
  • tried both but it didn't help.. i just think the ContainableBehavior is not even being loaded – vikc Feb 06 '14 at 01:18
  • There's a typo in your $actAs, which should be $actsAs. Otherwise your configuration looks exactly like mine so sorry can't help much further on why it isn't loading. – JamesLee Feb 06 '14 at 01:38
  • Oh my god!! I must be blind.. Ty Ty Ty.. So ashamed to see that stupid mistake, and of course it's these lame mistakes that make programmer's life miserable.. Ty again – vikc Feb 06 '14 at 02:16