Tuesday, December 15, 2009

Quirks With CakePHP

Hi there,

Lately at work we've been faced with a number of obscure problems with CakePHP. Here are some of the problems we've had and their solutions. Hope this helps someone who may be struggling with them out there.

1. Compact Changes your underscored variable names to CamelCased

$period_start = '07-15-2009';

.. in the view shows up as...


2. $this->Model->create() does not reset that models whitelist!

Say you do a $this->Model->saveField('name', 'rar'). That will then call the aftersave. CakePHP will automatically setup a whitelist array that will prevent it from editing other fields (except for id and modified). This is a nice feature, except when in that same models afterSave you want to go ahead and add a new one of those objects to the databae. First you call your $this->create() (you're in the model already) and then you $this->save($model); It won't work and will only try and save the name, id, and modified fields.
You can either manually reset the whitelist or create a new instance of the model like this $this->Model2 = new Model(); Then create that object on the new Model object, problem solved.

3. Containable won't let you un-contain very easily
It seems that once you've used containable on a model, that contain may be difficult to remove from future instances of that model, even when it was not the model that was originally contained, but one of its related models.

For example if we do this $this->Model->find('all', array('contain' => array('Child' => array('Child.name','Child.age'),'Acquaintance')));

We then later will have problems if we try and create a similar contain that wants additional fields.

$this->Model->find('all', array('contain' => array('Child' => array('Child.school','Child.hair_color'),'Acquaitnace')));

As above we can create a new instance of the model, which will clear up this problem. We believe we heard a fix for this was coming soon.

$this->Model2 = new Model();

No comments:

Post a Comment