ruvents/yii-behaviors

ActiveRecord 的实用行为。

0.0.12 2017-11-08 17:34 UTC

This package is auto-updated.

Last update: 2024-09-12 06:36:36 UTC


README

License

为 Yii 1.x ActiveRecord 模型提供的集合行为。

使用方法

composer.json 中添加依赖

"vizh/yii-behaviors": "0.0.6"

迁移

创建新表

$tableSchema = [
	'Id' => 'serial primary key',
	'PostId' => 'integer not null',
	'UserId' => 'integer not null',
	'Name' => 'varchar(1000) not null',
	'Body' => 'text not null'
];

$tableSchema = array_merge($tableSchema, DeletableBehavior::getMigrationFields());
$tableSchema = array_merge($tableSchema, UpdatableBehavior::getMigrationFields());

$this->createTable('BlogPost', $tableSchema);

向现有表添加字段

foreach (DeletableBehavior::getMigrationFields() as $column => $type) {
	$this->addColumn('BlogPost', $column, $type);
}

模型配置

public function behaviors()
{
	return [
		['class' => 'UpdatableBehavior'],
		['class' => 'DeletableBehavior'],
		['class' => 'AttributableBehavior', 'attribute' => 'Attributes']
	];
}

为了让 AttributableBehavior 正确工作,模型中需要有一个与行为配置中 'attribute' 参数值相同的公共属性。为了方便,可以将以下方法传递给模型:

/*
 * Методы AttributableBehavior
 * @method Role byAttribute($attribute, $value)
 * @method Role byAttributeExists($attribute)
 * @method Role byAttributeSearch($attribute, $value)
 */