yii-dream-team / yii2-lockable-activerecord
Pessimistic locking behavior for Yii2 ActiveRecord
Installs: 73 920
Dependents: 0
Suggesters: 0
Security: 0
Stars: 18
Watchers: 5
Forks: 4
Open Issues: 0
Type:yii-extension
Requires
- php: >=5.4.0
- yiisoft/yii2: *
README
This package allows you to use pessimistic locking (select for update) when you work with ActiveRecord models.
Installation
The preferred way to install this extension is through composer.
Either run
php composer.phar require --prefer-dist yii-dream-team/yii2-lockable-activerecord "*"
or add
"yii-dream-team/yii2-lockable-activerecord": "*"
to the require
section of your composer.json.
Usage
Attach the behavior to your controller class.
public function behaviors()
{
return [
'\yiidreamteam\behaviors\LockableActiveRecord',
];
}
Add @mixin phpdoc to you class definition.
/**
* Class Sample
* @package common\models
*
* @mixin \yiidreamteam\behaviors\LockableActiveRecord
*/
class Sample extends ActiveRecord { ... }
Use model locks in transaction.
$dbTransaction = $model->getDb()->beginTransaction(\yii\db\Transaction::SERIALIZABLE);
try {
$model->lock();
$model->doSomethingWhileLocked();
$dbTransaction->commit();
} catch(\Exception $e) {
$dbTransaction->rollBack();
throw $e;
}
Licence
MIT