, (*1)
cakephp-simple-scope
A simple cakephp behavior for scoping finds, (*2)
Background
I didn't want to write a bunch of custom finds so I just defined an array and made a behavior to interact with that array. Yep., (*3)
Requirements
Installation
[Using Composer], (*4)
Add the plugin to your project's composer.json
- something like this:, (*5)
{
"require": {
"josegonzalez/cakephp-simple-scope": "dev-master"
}
}
Because this plugin has the type cakephp-plugin
set in it's own composer.json
, composer knows to install it inside your /Plugins
directory, rather than in the usual vendors file. It is recommended that you add /Plugins/SimpleScope
to your .gitignore file. (Why? read this.), (*6)
[Manual], (*7)
[GIT Submodule], (*8)
In your app directory type:, (*9)
git submodule add git://github.com/josegonzalez/cakephp-simple-scope.git Plugin/SimpleScope
git submodule init
git submodule update
[GIT Clone], (*10)
In your plugin directory type, (*11)
git clone git://github.com/josegonzalez/cakephp-simple-scope.git SancSimpleScopetion
Enable plugin
In 2.0 you need to enable the plugin your app/Config/bootstrap.php
file:, (*12)
CakePlugin::load('SimpleScope');
If you are already using CakePlugin::loadAll();
, then this is not necessary., (*13)
Usage
Attach the behavior to your AppModel:, (*14)
Then define some scopes in your model:, (*15)
array(
'name' => 'Active admin users',
'find' => array(
'type' => 'list',
'virtualFields' => array(
'fullname' => "CONCAT(User.firstname, ' ', User.lastname)"
),
'options' => array(
'fields' => array('User.id', 'User.fullname'),
'conditions' => array('User.role LIKE' => '%admin%'),
'order' => array('User.fullname'),
),
),
),
);
}
?>
And then execute it:, (*16)
<?php
$activeUsers = $this->User->scopedFind('active_admin');
?>
You can alternatively use it as a custom model find:, (*17)
<?php
$activeUsers = $this->User->find('active_admin');
?>
You can also get a list of scopes:, (*18)
<?php
scopes = $this->User->scopes();
?>
Scopes:, (*19)
-
require a
name
string
-
optionally use a
find
array
Scope find
fields:, (*20)
-
require a
type
string
-
require an
options
array
-
optionally use a
virtualFields
field
Todo
License
The MIT License (MIT), (*21)
Copyright (c) 2014 Jose Diaz-Gonzalez, (*22)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:, (*23)
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software., (*24)
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE., (*25)