github
Advanced Search
  • Home
  • Pricing and Signup
  • Explore GitHub
  • Blog
  • Login

netguru / namedscopebehavior

  • Admin
  • Watch Unwatch
  • Fork
  • Your Fork
  • Pull Request
  • Download Source
    • 10
    • 1
  • Source
  • Commits
  • Network (1)
  • Issues (0)
  • Downloads (0)
  • Wiki (1)
  • Graphs
  • Branch: master

click here to add a description

click here to add a homepage

  • Branches (1)
    • master ✓
  • Tags (0)
Sending Request…
Enable Donations

Pledgie Donations

Once activated, we'll place the following badge in your repository's detail box:
Pledgie_example
This service is courtesy of Pledgie.

Behavior for CakePHP that allows use of named scopes with model's find() method. — Read more

  cancel

http://codetunes.com/2008/09/05/named-scope-in-cakephp/

  cancel
  • Private
  • Read-Only
  • HTTP Read-Only

This URL has Read+Write access

Fixed scoping when doing find('first') on models with $model->id set. 
szajbus (author)
Sat Feb 14 13:06:35 -0800 2009
commit  623a7a4d744eac5e4f0582a461c4a73c85e45c05
tree    8a3e8cdee930cd34769045c2704f94ab5b795b07
parent  71edd461a2a3b0edb0908200062345fc32b81f19
namedscopebehavior /
name age
history
message
file MIT-LICENSE Loading commit data...
file README
file README.rdoc
file named_scope.php
README.rdoc

NamedScopeBehavior

This is a behavior for CakePHP that allows use of named scopes with model’s find() method. What it does it translating defined scopes to conditions before extecuting find query.

Installation

Download and place named_scope.php file in app/models/behaviors folder of your CakePHP application.

Usage

Example of User model:

$actsAs = array(

  'NamedScope' => array(
    'activated' => array('User.activated' => 1),
    'online' => array('date_add(User.last_activity, interval 5 minute) > now()')
  )

);

Then in your controller (or anywhere else) you can do:

$this->User->find(‘all’, array(‘scopes’ => array(‘activated’)));

which is an equivalent to:

$this->User->find(‘all’, array(‘conditions’ => array(‘User.activated’ => 1)));

You can use many scopes at once and mix them with usually defined conditions:

$this->User->find(‘all’, array(‘condtions’ => array(‘User.admin’ => 1), ‘scopes’ => array(‘activated’, ‘online’)))

If you have associated models (hasOne or belongsTo) and they have named scopes defined you can use them all in one find query: $this->User->find(‘all’, array(‘scopes’ => (‘activated’, ‘Group.admins’))) // assuming that User belongsTo Group and Group model has ‘admins’ named scope defined.

Scopes works also with pagination (in your controller):

$paginate = array(

  'User' => array(
    'order' => 'created ASC',
    'limit' => 20,
    'scopes' => array('online', 'activated')
  )

)

Copyright

Copyright © 2008 Michal Szajbe (codetunes.com) and netguru (netguru.pl), released under the MIT license

Blog | Support | Training | Contact | API | Status | Twitter | Help | Security
© 2010 GitHub Inc. All rights reserved. | Terms of Service | Privacy Policy
Powered by the Dedicated Servers and
Cloud Computing of Rackspace Hosting®
Dedicated Server