1 # Collectionable Plugin #
4 This is a utility plugin for CakePHP. This helps managing find options, virtualFields and validations.
7 - Define $options(such a property name can be modified by configure) for Options Behavior
8 - Define $virtualFieldsCollection(such a property name can be modified by configure) for VirtualFields Behavior
9 - Define 'Validation'(such a config name can be modified by configure) section into Configure for ConfigValidationBehavior
10 - Define $validate{PatternName}, like $validateAdd, same structure with $validate, for MultiValidationBehavior
14 ### OptionsBehavior ###
16 Here is a simple Post Model.
17 class Post extends AppModel {
18 var $hasMany = array('Comment');
19 var $hasOne = array('Status');
21 var $acsAs = array('Collectionable.options');
22 var $defaultOption = true; // or string like 'default'
33 'condtiions' => array('Status.published' => true),
36 'order' => ('Post.updated DESC'),
45 // You can do sub merging
54 You can use them by like:
55 class PostsController extends AppController {
57 $this->paginate = $this->Post->options('index');
58 $this->set('posts', $this->paginate());
62 $this->paginate = $this->Post->options('index', 'rss'); // multiple merging at run time;
63 $this->set('posts', $this->paginate());
66 function all_in_one_page() {
67 // you can use "options" attribute wihtin finding options
68 $posts = $this->Post->find('all', array('options' => array('index', 'unlimited')));
69 $this->set(compact('posts'));
73 To see more syntax, you would look at [the test case](http://github.com/hiromi2424/Collectionable/blob/master/tests/cases/behaviors/options.test.php) or [the code](http://github.com/hiromi2424/Collectionable/blob/master/models/behaviors/options.php).
75 ### VirtualFieldsBehavior ###
77 This sample uses [MatchableBehavior](http://github.com/hiromi2424/MatchableBehavior).
79 class User extends AppModel {
80 var $hasMany = array('Post');
81 var $actsAs = array('Collectionable.VirtualFields', 'Matchable');
83 var $virtualFields = array(
84 'full_name' => "CONCAT(User.first_name, ' ', User.last_name)",
86 var $virtualFieldsCollection = array(
87 'posts_count' => 'COUNT(Post.id)',
88 'amount_used' => 'SUM(Post.volume)',
92 You can use them by like:
95 class UsersController extends AppController {
96 function admin_index() {
97 // Hey, you may feel like using OptionsBehavior :P
98 $jointo = array('Post');
100 $virtualFields = array('posts_count', 'amount_used'); // key of collections
101 $this->paginate = compact('jointo', 'group', 'virtualFields');
102 $this->set('users', $this->paginate());
106 $virtualFields = array('full_name' => false); // disable virtualFields
107 $user = $this->User->find('first', compact('virtualFields'));
108 $this->set(compact('user'));
112 ### ConfigValidationBehavior ###
115 class User extends AppModel {
116 var $actsAs = array('Collectionable.ConfigValidation');
118 var $validate = array(
121 'rule' => array('notempty'),
124 'rule' => array('minlength'),
125 'message' => 'I said more than %s!!',
130 'rule' => array('notempty'),
133 'rule' => array('between'),
139 You can set validation parameters, messages from Configuration:
142 Configure::write('Validation', array(
143 'parameters' => array(
149 'among' => array(16, 256)
155 'required' => 'you need to enter.',
156 'min' => '%s characters needed',
160 'required' => 'are you kidding me or misreading?',
167 Note that priority is "hard coded on your model" > "specifying Model and field" > "default".
168 But if you turn $overwrite property on, "specifying Model and field" forces to overwrite("default" does not).
171 ### MultiValidationBehavior ###
174 class User extends AppModel {
176 var $actsAs = array('Collectionable.MultiValidation');
178 var $validate = array(
179 'password_raw' => array(
181 'rule' => array('notempty'),
183 'minlength' => array(
184 'rule' => array('minlength', 6),
189 // note that $validateprofile is invalid with 'profile'
190 var $validateProfile = array(
193 'rule' => array('notempty'),
195 'maxlength' => array(
196 'rule' => array('maxlength', 40),
201 var $validateRequireEmail = array(
204 'rule' => array('notempty'),
207 'rule' => array('email'),
212 var $validatePasswordConfirm = array(
213 'password_confirm' => array(
215 'rule' => array('notempty'),
217 'confirm_password' => array(
218 'rule' => array('confirm_password'),
223 // You can set validation pattern on demand:
224 function add($data, $validate = true, $options = array()) {
225 $this->useValidationSet('requireEmail');
227 return $this->save($data, $validate, $options);
230 // You can dsiable default $validate with second argument as false:
231 function edit($data, $validate = true, $options = array()) {
232 $this->useValidationSet('profile', false);
233 return $this->save($data, $validate, $options);
236 // You can specify two and more rule sets. these will be merged
237 function resetEmail($data) {
238 $this->useValidationSet(array('requireEmail', 'passwordConfirm'));
241 function confirm_password() {
249 - [nojimage](http://github.com/nojimage) created [base of this plugin](http://github.com/nojimage/paging)
254 Licensed under The MIT License.
255 Redistributions of files must retain the above copyright notice.
258 Copyright 2010 hiromi, https://github.com/hiromi2424
260 Permission is hereby granted, free of charge, to any person obtaining a copy
261 of this software and associated documentation files (the "Software"), to deal
262 in the Software without restriction, including without limitation the rights
263 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
264 copies of the Software, and to permit persons to whom the Software is
265 furnished to do so, subject to the following conditions:
267 The above copyright notice and this permission notice shall be included in
268 all copies or substantial portions of the Software.
270 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
271 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
272 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
273 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
274 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
275 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN