Really useful software project management and source code hosting.    tell me more...
BROWSE: projects / users / groups
A CakePHP behavior that allows you to define named scopes for a model, and then apply them to any find call.
star_disabled Dashboard Source Tickets Wiki Blog Network


Really Useful Social coding!

Codaset is an open system, so you can browse and search through all the open source projects, and check out what your friends are coding. Follow them, befriend them, and fork their code; quickly and easily.
Every single open source project you create is free, so come on and use Codaset at no cost. Your first private or semi-private project is also free. Read more about what it costs after that.

Git clone URL's...

Public: git://codaset.com/joelmoss/cakephp-named-scope.git
Active tickets:
1
Milestones:
0
Bookmarks:
8
Forks:
0


NamedScope Behavior for CakePHP

This NamedScope behavior for CakePHP allows you to define named scopes for a model,
and then apply them to any find call. It will automagically create a model method,
and a method for use with the _findMethods property of the model.

NamedScope is now packaged as a plugin, so as to allow easier management - particularly with Git submodules - and to support tests.

Get it from http://github.com/joelmoss/cakephp-namedscope or http://developwithstyle.com

Borrowed from an original idea found in Ruby on Rails, and a first attempted for Cake
by MichaƂ Szajbe http://github.com/netguru/namedscopebehavior

Install:

Just create a 'named_scope' directory in your app/plugins directory, and drop these files and directories into it.

If your application is managed via Git, you can use NamedScope as a submodule. Just cd into your app directory, and run:

git submodule add git://github.com/joelmoss/cakephp-namedscope.git plugins/named_scope

Once the submodule is added we need to register this submodule using init:

git submodule init

From now on we can update all the submodules using the following command:

git submodule update

Example:

I have a User model and want to return only those which are active. So I define this in my model:

var $actsAs = array(
    'NamedScope.NamedScope' => array(
        'active' => array(
            'conditions' => array(
                'User.is_active' => true
            )
        )
    )
);

Then call this in my User controller:

$active_users = $this->User->active('all');

or this:

$active_users = $this->User->find('active');

You can even pass in the standard find params to both calls:

$active_users = $this->User->active('all', array(
    'conditions' => array(
        'User.created' => '2008-01-01'
    ),
    'order' => 'User.name ASC'
));

or:

$active_users = $this->User->find('active', array(
    'conditions' => array(
        'User.created' => '2008-01-01'
    ),
    'order' => 'User.name ASC'
));