PHPackages                             hiromi2424/collectionable - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. hiromi2424/collectionable

ActiveCakephp-plugin[Utility &amp; Helpers](/categories/utility)

hiromi2424/collectionable
=========================

Collectionable plugin for CakePHP

172.3k6PHP

Since Mar 5Pushed 12y ago3 watchersCompare

[ Source](https://github.com/hiromi2424/Collectionable)[ Packagist](https://packagist.org/packages/hiromi2424/collectionable)[ RSS](/packages/hiromi2424-collectionable/feed)WikiDiscussions cake2 Synced 1mo ago

READMEChangelogDependenciesVersions (1)Used By (0)

Collectionable Plugin
=====================

[](#collectionable-plugin)

[![Build Status](https://camo.githubusercontent.com/76d8af18a74932553cbb4ca2e16cbc810ddea2f94ec0cd759c832a5bb5989fcb/68747470733a2f2f7472617669732d63692e6f72672f6869726f6d69323432342f436f6c6c656374696f6e61626c652e706e673f6272616e63683d63616b6532)](https://travis-ci.org/hiromi2424/Collectionable)

Introduction
------------

[](#introduction)

This is a utility plugin for CakePHP. This helps managing find options, virtualFields and validations.

Setup
-----

[](#setup)

- Define $options(such a property name can be modified by configure) for Options Behavior
- Define $virtualFieldsCollection(such a property name can be modified by configure) for VirtualFields Behavior
- Define 'Validation'(such a config name can be modified by configure) section into Configure for ConfigValidationBehavior
- Define $validate{PatternName}, like $validateAdd, same structure with $validate, for MultiValidationBehavior

Examples
--------

[](#examples)

### OptionsBehavior

[](#optionsbehavior)

Here is a simple Post Model.

```
class Post extends AppModel {
	public $hasMany = array('Comment');
	public $hasOne = array('Status');

	public $actsAs = array('Collectionable.Options');
	public $defaultOption = true; // or string like 'default'

	public $options =array(
		'default' => array(
			'contain' => array(
				'Comment',
				'Status',
			),
		'limit' => 10,
		),
		'published' => array(
			'condtiions' => array('Status.published' => true),
		),
		'recent' => array(
			'order' => ('Post.updated DESC'),
		),
		'rss' => array(
			'limit' => 15,
		),
		'unlimited' => array(
			'limit' => null,
		),
		'index' => array(
			// You can do sub merging
			'options' => array(
				'published',
				'recent',
			),
		),
	);
}

```

You can use them by like:

```
class PostsController extends AppController {
	public function index() {
		$this->paginate = $this->Post->options('index');
		$this->set('posts', $this->paginate());
	}

	public function rss() {
		$this->paginate = $this->Post->options('index', 'rss'); // multiple merging at run time;
		$this->set('posts', $this->paginate());
	}

	public function all_in_one_page() {
		// you can use "options" attribute wihtin finding options
		$posts = $this->Post->find('all', array('options' => array('index', 'unlimited')));
		$this->set(compact('posts'));
	}
}

```

To see more syntax, you would look at [the test case](https://github.com/hiromi2424/Collectionable/blob/cake2/Test/Case/OptionsBehaviorTest.php) or [the code](https://github.com/hiromi2424/Collectionable/blob/cake2/Model/Behavior/OptionsBehavior.php).

### VirtualFieldsBehavior

[](#virtualfieldsbehavior)

This sample uses [MatchableBehavior](http://github.com/hiromi2424/MatchableBehavior).

```
class User extends AppModel {

	public $hasMany = array('Post');
	public $actsAs = array('Collectionable.VirtualFields', 'Matchable');

	public $virtualFields = array(
		'full_name' => "CONCAT(User.first_name, ' ', User.last_name)",
	);
	public $virtualFieldsCollection = array(
		'posts_count' => 'COUNT(Post.id)',
		'amount_used' => 'SUM(Post.volume)',
	);

}

```

You can use them by like:

```
class UsersController extends AppController {

	public function admin_index() {
		// Hey, you may feel like using OptionsBehavior :P
		$jointo = array('Post');
		$group = 'User.id';
		$virtualFields = array('posts_count', 'amount_used'); // key of collections
		$this->paginate = compact('jointo', 'group', 'virtualFields');
		$this->set('users', $this->paginate());
	}

	public function profile() {
		$virtualFields = array('full_name' => false); // disable virtualFields
		$user = $this->User->find('first', compact('virtualFields'));
		$this->set(compact('user'));
	}

	public function profile_ja() {
		// The order of parts for person name in Japanese is alternative compared with English.
		$virtualFields = array(
			'full_name' => "CONCAT(User.last_name, ' ', User.first_name)", // overriding
			'phonetic_full_name' => "CONCAT(User.phonetic_last_name, ' ', User.phonetic_first_name)", // dynamic adding
		);
		$user = $this->User->find('first', compact('virtualFields'));
		$this->set(compact('user'));
	}
}

```

### ConfigValidationBehavior

[](#configvalidationbehavior)

```
class User extends AppModel {

	public $actsAs = array('Collectionable.ConfigValidation');

	public $validate = array(
		'nickname' => array(
			'required' => array(
				'rule' => array('notempty'),
			),
			'min' => array(
				'rule' => array('minlength'),
				'message' => 'I said more than %s!!',
			),
		),
		'email' => array(
			'required' => array(
				'rule' => array('notempty'),
			),
			'among' => array(
				'rule' => array('between'),
			),
		),
	);
}

```

You can set validation parameters, messages from Configuration:

```
Configure::write('Validation', array(
	'parameters' => array(
		'User' => array(
			'nickname' => array(
				'min' => 3,
			),
			'email' => array(
				'among' => array(16, 256)
			),
		),
	),
	'messages' => array(
		'default' => array(
			'required' => 'you need to enter.',
			'min' => '%s characters needed',
		),
		'User' => array(
			'email' => array(
				'required' => 'are you kidding me or misreading?',
			),
		),
	),
));

```

Note that priority is "hard coded on your model" &gt; "specifying Model and field" &gt; "default". But if you turn $overwrite property on, "specifying Model and field" forces to overwrite("default" does not).

### ConfigValidationBehavior

[](#configvalidationbehavior-1)

```
class User extends AppModel {

	public $actsAs = array('Collectionable.MultiValidation');

	public $validate = array(
		'password_raw' => array(
			'required' => array(
				'rule' => array('notempty'),
			),
			'minlength' => array(
				'rule' => array('minlength', 6),
			),
		),
	);

	// note that $validateprofile is invalid with 'profile'
	public $validateProfile = array(
		'nickname' => array(
			'required' => array(
				'rule' => array('notempty'),
			),
			'maxlength' => array(
				'rule' => array('maxlength', 40),
			),
		),
	);

	public $validateRequireEmail = array(
		'email' => array(
			'required' => array(
				'rule' => array('notempty'),
			),
			'email' => array(
				'rule' => array('email'),
			),
		),
	);

	public $validatePasswordConfirm = array(
		'password_confirm' => array(
			'required' => array(
				'rule' => array('notempty'),
			),
			'confirm_password' => array(
				'rule' => array('confirm_password'),
			),
		),
	);

	public function add($data, $validate = true, $options = array()) {

		// You can set validation pattern on demand:
		$this->useValidationSet('requireEmail');
		$this->create();

		return $this->save($data, $validate, $options);

	}

	public function edit($data, $validate = true, $options = array()) {

		// You can dsiable default $validate with second argument as false:
		$this->useValidationSet('profile', false);
		return $this->save($data, $validate, $options);
	}

	public function resetEmail($data) {

		// You can specify two and more rule sets. these will be merged
		$this->useValidationSet(array('requireEmail', 'passwordConfirm'));

	}

	public function confirm_password() {
		// confirm password
	}

}

```

You can also use magick method like:

```
$this->useProfileValidation();
$this->useRequireEmailAndPasswordConfirmValidation(); // too long :P

```

Thanks
------

[](#thanks)

- [nojimage](http://github.com/nojimage) created [base of this plugin](http://github.com/nojimage/paging)

License
-------

[](#license)

Licensed under The MIT License. Redistributions of files must retain the above copyright notice.

Copyright 2011 hiromi,

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.

###  Health Score

28

—

LowBetter than 54% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity28

Limited adoption so far

Community14

Small or concentrated contributor base

Maturity41

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 80% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

### Community

Maintainers

![](https://www.gravatar.com/avatar/4fe1202888a1e909332ee0eda4eb3c017e5ae58912e9777146c97bb61f126ee8?d=identicon)[hiromi2424](/maintainers/hiromi2424)

---

Top Contributors

[![hiromi2424](https://avatars.githubusercontent.com/u/191297?v=4)](https://github.com/hiromi2424 "hiromi2424 (20 commits)")[![nojimage](https://avatars.githubusercontent.com/u/100564?v=4)](https://github.com/nojimage "nojimage (3 commits)")[![konnoyosuke](https://avatars.githubusercontent.com/u/185094?v=4)](https://github.com/konnoyosuke "konnoyosuke (2 commits)")

### Embed Badge

![Health badge](/badges/hiromi2424-collectionable/health.svg)

```
[![Health](https://phpackages.com/badges/hiromi2424-collectionable/health.svg)](https://phpackages.com/packages/hiromi2424-collectionable)
```

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
