A Thunderbolt for a developer! Do you know any good developers based in The Netherlands? Tell them about working at Springest. If we hire them, you will get a 27" Apple Thunderbolt Display. :)

Named Scopes in CakePHP

Ruby on Rails 2.1 introduced the concept of named scopes. During a friday afternoon we discussed named scopes and wanted to have it available for us to use. We ended up modifying an existing behavior for CakePHP to make it a little bit easier to use.

Named scopes are essentially a shorthand for a frequently used set of conditions you need when you want to query your model. So if you have a User model and you want to find all the active users which are named “Joe”, you would normally end up with this:

$this->User->find(array('conditions' => array('is_active' => 1, 'name' => 'Joe')));

Using named scopes you can write it like this:

$this->User->active->find(array('conditions' => array('name' => 'Joe')));

Which is not only shorter to write, but also easier to read. And it also leads to better maintainable code.

Our modified behavior requires you to modify your app_model (Without it, the $this->User->active shorthand will not work). You can read the installation and usage instructions on github, where you can also find the code. Enjoy!