josegonzalez/cakephp-simple-scope

A simple cakephp behavior for scoping finds.

1.1.0 2014-02-15 21:51 UTC

This package is auto-updated.

Last update: 2024-10-29 04:19:55 UTC


README

Build Status Coverage Status Total Downloads Latest Stable Version Documentation Status Gratipay

cakephp-simple-scope

A simple cakephp behavior for scoping finds

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.

Requirements

  • CakePHP 2.x

Installation

[Using Composer]

Add the plugin to your project's composer.json - something like this:

{
	"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.)

[Manual]

[GIT Submodule]

In your app directory type:

git submodule add git://github.com/josegonzalez/cakephp-simple-scope.git Plugin/SimpleScope
git submodule init
git submodule update

[GIT Clone]

In your plugin directory type

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:

	CakePlugin::load('SimpleScope');

If you are already using CakePlugin::loadAll();, then this is not necessary.

Usage

Attach the behavior to your AppModel:

<?php
App::uses('Model', 'Model');

class AppModel extends Model
{
    public $actsAs = array('SimpleScope.Scope');
}
?>

Then define some scopes in your model:

<?php
App::uses('AppModel', 'Model');

class User extends AppModel
{
    public $scopes = array(
        'active_admin' => 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:

<?php
$activeUsers = $this->User->scopedFind('active_admin');
?>

You can alternatively use it as a custom model find:

<?php
$activeUsers = $this->User->find('active_admin');
?>

You can also get a list of scopes:

<?php
scopes = $this->User->scopes();
?>

Scopes:

  • require a name string
  • optionally use a find array

Scope find fields:

  • require a type string
  • require an options array
  • optionally use a virtualFields field

Todo

  • Unit Tests

License

The MIT License (MIT)

Copyright (c) 2014 Jose Diaz-Gonzalez

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:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

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.