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();

Saturday, March 14, 2009

Cures for JQuery UI Slowness

Cures for JQuery UI slowness

I have noticed that JQuery UI is particularly slow in Firefox. This affects me both when using the code on my site and when viewing their own page. It dawned on me recently that the cause of this was the fact that I tend to leave firebug on.

Secondly, I've noticed that when possible it is a heck of a lot faster to use the browser's native sliding feature (by say turning scrollbars on in a div using  overflow=auto or overflow=scroll and setting a hard width) than using JQuery UI's sliding. 

Thirdly, because of the overhead of the JQuery UI I have found it faster in many cases just to write my own logic that more specifically fits the needs of the application at hand. Using the UI to get some ideas, but then applying my own logic to the problem at hand.

So for a re-cap of how to speed up building UI tools with JQuery:
  • Disable Firebug (and encourage your visitors to)
  • Use Native Scrolling when Possible
  • Write your own code

Wednesday, March 11, 2009

This is my blog for coding.

Lately I've been playing with:
- JQuery 1.3.2
- CakePHP

Hopefully you'll hear about all of my endeavors with these languages shortly!


Jamund Ferguson