hiromi2424/collectionable

CakePHP 的 Collectionable 插件

安装数: 1,950

依赖者: 0

建议者: 0

安全: 0

星标: 18

关注者: 4

分支: 6

开放问题: 0

类型:cakephp-plugin

dev-cake2 2014-03-05 06:48 UTC

This package is not auto-updated.

Last update: 2024-09-24 06:16:26 UTC


README

Build Status

简介

这是一个 CakePHP 的实用插件。它帮助管理查找选项、虚拟字段和验证。

设置

  • 为选项行为定义 $options(可以通过配置修改此属性名)
  • 为虚拟字段行为定义 $virtualFieldsCollection(可以通过配置修改此属性名)
  • 在配置中定义 'Validation'(可以通过配置修改此配置名)部分,用于 ConfigValidationBehavior
  • 为多验证行为定义 $validate{PatternName},如 $validateAdd,结构与 $validate 相同

示例

OptionsBehavior

这是一个简单的 Post 模型。

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',
			),
		),
	);
}

您可以使用它们,例如

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'));
	}
}

要查看更多语法,请参阅 测试用例代码

VirtualFieldsBehavior

此示例使用 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)',
	);

}

您可以使用它们,例如

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

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'),
			),
		),
	);
}

您可以通过配置设置验证参数和消息

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?',
			),
		),
	),
));

请注意,优先级是“硬编码在您的模型”>“指定模型和字段”>“默认”。但如果您开启 $overwrite 属性,则“指定模型和字段”将强制覆盖(“默认”不会)。

ConfigValidationBehavior

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
	}

}

您还可以使用魔法方法,例如

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

感谢

许可

许可协议为 MIT 许可证。文件重新分发必须保留上述版权声明。

版权所有 2011 hiromi, https://github.com/hiromi2424

在此条件下,任何人获得此软件及其相关文档(“软件”)的副本,均免费获得在此软件上操作的权利,不受任何限制,包括但不限于使用、复制、修改、合并、发布、分发、转授许可和/或销售软件副本的权利,并允许向软件提供者提供软件的人士行使其权利,前提是

上述版权声明和本许可协议应包含在软件的所有副本或实质性部分中。

本软件按“原样”提供,不提供任何明示或暗示的保证,包括但不限于适销性、针对特定目的的适用性和非侵权性。在任何情况下,作者或版权所有者均不对任何索赔、损害或其他责任负责,无论是基于合同、侵权或其他原因,无论是否因软件或其使用或其他方式引起。