NamedScope for CakePHP

20 Nov 2008 In: CakePHP

I discovered the joys of NamedScope for Ruby on Rails quite a while ago now, and have always been an admirer. Its makes performing finds on models very elegant and convenient, by automagically creating model methods based on your pased scope params. Just like this:

Named Scope in Rails
  1. class User < ActiveRecord::Base
  2.   named_scope :active, :conditions => {:active => true}
  3.   named_scope :inactive, :conditions => {:active => false}
  4. end
  5.  
  6. # Standard usage
  7. User.active    # same as User.find(:all, :conditions => {:active => true})
  8. User.inactive # same as User.find(:all, :conditions => {:active => false})

Then a few months ago I read this blog post and discovered that someone had created similar functionality for CakePHP, in the form of a model behavior. Although it’s not quite as powerful as it’s Rails cousin, it does let you define named scopes for any model quite easily. However, I wasn’t crazy about a few things.

Named Scopes are defined like this:

Don't like this:
  1. class User extends AppModel {
  2.   var $actsAs = array(
  3.     'NamedScope' => array(
  4.       'activated' => array('User.activated in not null'),
  5.       'online' => array('date_add(User.last_activity, interval 5 minute) > now()')
  6.     )
  7.   );
  8. }

The above format means we can only pass find conditions to a named scope, and cannot pass any other params, such as ORDER and FIELDS.

Then a named scope is then called like this:

this is not nice either
  1. $this->User->find('all', array('scope' => 'activated'));
  2. $this->User->find('all',
  3.   array('conditions' => 'points > 10', 'scope' => array('activated', 'online')))

I have to pass enough params to a find call as it is, so I don’t want anymore.

What I want to do is this:

This is better
  1. class User extends AppModel {
  2.   var $actsAs = array(
  3.     'NamedScope' => array(
  4.       'active' => array(
  5.         'conditions' => array(
  6.           'User.is_active' => true
  7.         ),
  8.         'order' => 'User.name ASC'
  9.       )
  10.     )
  11.   );
  12. };

and this

Easy peasy!
  1. $active_users = $this->User->active('all');

I can even do this:

This is just lovely
  1. $active_users = $this->User->active('all', array(
  2.     'conditions' => array(
  3.         'User.created' => '2008-01-01'
  4.     ),
  5.     'order' => 'User.name ASC'
  6. ));

Much improved I think, and so much more powerful. So I’ve only gone and coded the damn thing! You can find my version of Named Scope for Cake on Github at http://github.com/joelmoss/cakephp-namedscope.

And you know what? Thanks to the power of CakePHP, the actual code is seven lines shorter than the aforementioned version.

The power of plugins

13 Nov 2008 In: CakePHP, DB Migrations

Since I discovered the DebugKit plugin for CakePHP that is currently in early development by Mark Story and others (including me. well, one commit still means I helped ;)), I have been left rocked about the power of plugins. I always thought that plugins were simply a way to create a Cake app that can be plugged in to an existing application. But It seems I underestimated their power.

So I began to think about how I could refactor Migrations into a plugin, rather than a bunch of files that you drop into your vendors directory. And that took me all of 20 seconds! I didn’t have to change a thing. Thanks to this latest commit, you can now use a shell script from any plugin.

So I just drop the migrations directory directly into my plugins directory, and wham bam thank you maam! I created my first Cake plugin.

Of course, I can still drop the migrations shells into my global vendors/shells directory, but I just like the way I can plug it in.

So a plugin is now the recommended way to use Migrations.

Keeping your core.php DRY

5 Nov 2008 In: CakePHP

Something that I have been taking advantage of in my Cake 1.2 apps for quite a while now, is the ability to separate your custom config options from your app’s core.php file. And I am pretty sure that it is one of those lesser know features in 1.2.

The core.php file within your app’s config directory is used to store application wide configuration variables. Things like your debug level, cache settings and sessions store are set here using Cake’s very useful Configure class. This file can start to get quite large and unruly as the development of your app progresses. So one of the first things I do when I start a new app, is to separate my custom application config variable into another file within the config directory. This then keeps the core Cake configure settings separate from your custom ones, and keeps things a little dry.

Just create a new file in your app/config directory. You can name this anything you want, but I usually go with something like ‘config.php’ or ‘app.php’. As long as it ends with ‘.php’, it doesn’t matter.

Then in your app/config/core.php file, add the following line to the top of the file:

Configure::load('config');

As long as you pass the name of your newly created file, then it will be fine. So the above will work if I created a file called app/config/config.php.

Then within your new config file, you can set any config variables you wish. However, you don’t use the familar Configure::write('name', 'value'); convention. The variables that you set within your custom config file should be specified using arrays. Like this:

$config['name'] = ‘value’;

All variables in this file only, should set within the $config array as array name/value pairs. If your custom config file is called ‘app.php’, then the array name would be $app. So effectively, you could create as many of these custom config files as you wish. Perhaps if you had distinct sections of the app. I usually just create one, so I can easily access my custom config vars, without worrying about Cake’s built in config vars.

You access these custom config variables in the exact same way as you would any of the other built in core config vars.

Configure::read('name');

Enjoy!

DB Migrations moves home

2 Nov 2008 In: DB Migrations

I’ve been slowly moving all my public and private projects over to Git and Github whenever, and whereever I can. I’ve already created a mirror of the CakePHP 1.2 branch at Github, and I have a number of my private projects up too.

So I just completed the switch for CakePHP DB Migrations. The source is now available on Github and is open to any pull requests. If you have any funky new features, or really need a bug fixed quickly, please feel free to fork the project. But please remember to send me a pull request.

I also moved the Google Code wiki into Github, and enhanced it a little. The wiki now includes some YAML migration examples, and will be expanded with more info and tutorials as and when I can.

The final step of the move, are the tickets/issues. And because it works so nicely with Github, I decided upon using Lighthouse. So any bug reports, and/or feature requests should now be sent to Lighthouse.

Thanks again for all the support. My hope in moving to Github, is that I can enhance the community aspect of this project, and to encourage more involvement from those of you who use it. I’m all ears!

Not posted much here recently, so It’s great that all of a sudden I have lots of Cake related news and tidbits to share with you all. So hopefully, this will be post number one in a long series of posts covering CakPHP related news and items, entitled Cake Nibbles. Enjoy!

PHPShop

The Google Group revealed some interesting news about a well know open source PHP shopping cart application. It appears that the next version of PHPShop will be built with CakePHP 1.2. That can be nothing but positive for Cake Land.

Google No. 1

Search for “PHP framework” on Google, and guess what? Cake is now number one! Oh yes, NUMBER 1! Top of the pops!

Debug Kit

Not content with being one of the biggest contributors to the core, Mark Story has created a plugin for CakePHP to help with debugging your applications. The Cake DebugKit, which is now publicly available at GitHub, is similar to the extremely helpful Debug Bar that can be found in the Symfony framework. It displays at the top right of your screen, and shows you info about your app, such as load time and SQL queries.

It’s early days, but its a great start, and is something that is sorely needed in Cake. I hope to help out with it myself. And the idea of including it in a plugin is genius, and has made me realise how powerful the plugin architecture has become in 1.2

TextMate Bundle

Also on GitHub, you can now find another version of the Cake TextMate bundle. But this one is somewhat official, in that it is being maintained by Nate; Cake’s Lead Dev.

So thats it for this issue of Cake Nibbles, but I hope to post again soon.

If you have any interesting, or non-interesting news in Cake land, please let me know.

Who is Developing with Style?

My name is Joel Moss, a web developer and all round nice guy, living in Manchester, England. I am currently working full time for ShermansTravel.com, but I fill whatever spare time I have with lots of good and wholesome "stuff"! Like developing my own ideas; such as Tooum, contributing to the excellent CakePHP framework, and doing more work for ShermansTravel.

So this is my blog - my soap box! Here I attempt to share my likes, my dislikes, and my opinions. As well as providing some occasional respite from the daily crap we all endure. Enjoy ;)

Hey, if you want to reach me, i'm available via email:joel[at]developwithstyle[dot]com, and AIM:joelkmoss.


Twittering...

Categories

Archives