This blog is about the PHP solutions I have to think of when I'm developing systems almost every single day...

Friday, December 5, 2008

New things in cakePHP core

Quite a while back I updated the cakePHP core in MyMeeting. Because of that update I broke so many things in MyMeeting because of some new ways cakePHP implements stuff. Here is a summary of some of the changes I had to make to make it work again.

Complex query is by itself now
This is the single most important update which broke a lot of stuff in MyMeeting. So now you cannot do things like:


$this->mystuff->find(array('conditions'=>array('updated'=>'<=!-now())))

That won't work anymore. !- no longer stops the value from being put in quotes. Now you've got to do this way:

$this->mystuff->find(array('conditions'=>array('updated <= now()')))

That would work. No need to asssign value to key and stuff. Just what is the query. Same thing that this won't work anymore:

$this->mystuff->find(array('conditions'=>array('deleted'=>'!=1')))

It now has to be:

$this->mystuff->find(array('conditions'=>array('deleted != 1')))

Just as some example of how the new queries should be.

Unicode is broken in some of the older system and new cakePHP uses unicode in validation

This kinda caused me a lot of grief. Validation worked very well on my dev machine which ran on Ubuntu. But as soon as I moved it to production server (CentOS & Mandriva) it didn't work anymore. As it turned out that in the pcre library, unicode support is not enabled by default. To counter this I had to change back the regex not to use unicode and just use normal regex. So I changed the file cake/libs/validation.php in the function alphaNumeric the regex is from using unicode:

$_this->regex = '/^[\p{Ll}\p{Lm}\p{Lo}\p{Lt}\p{Lu}\p{Nd}]+$/mu';

to not use unicode:

$_this->regex = '/^[a-zA-Z0-9_-]+$/mu';

It now works on all systems but I've got to be more carefull now whenever I upgrade cakePHP core so that I do the necessary hack (this is a hack. A proper implementation would detect whether there is unicode support and use the approriate regex then)

Core now have contain. No more need for the Bindable behaviour to be added


Using contain which comes with the core would better guarantee that it'd be properly supported in the future. The syntax is almost the same. So now whenever you need to restrict what models are queried or maybe even drop off all association so that only the specified model is queried, contain is the way to do it. How to drop off all of the associations? Like this:

$this->mymodel->find(array('contain'=>false))

Of course you can put in conditions and stuff, but basically setting false to contain would query only the table for that model. Neat.

Unit tests rocks

I can't say this enough. Unit test allows for very quick testing of whether things work or not. And actually it would (in the future) easily catch errors which might be caused by changes in the query syntax and stuff. To learn how to set up unit test you can go to http://bakery.cakephp.org/articles/view/testing-models-with-cakephp-1-2-test-suite. To access unit tests you need to enable debug in app/config/core.php. And then access the test page by going to http://location-of-app/test.php. Over there you'll see a whole lot of tests already provided to test cakePHP core functions. And then you can add new test cases for your app by adding the file model.test.php into app/tests/cases/model. You can add fixtures to your test by adding model_fixture.php to your app/tests/fixtures folder.

1 comments:

Anonymous said...

[URL=http://porntube2.org][color=red][b]Watch Free Porn Videos Online (BEST WAY 100% FREE PORN)[/b][/color][/URL]


[url=http://porntube2.org][img]http://porntube2.org/teen.jpg[/img][/url]

About Me

My Photo
Abdullah Zainul Abidin
I am what I am because of what I am...
View my complete profile