yii2-lockable-activerecord This package allows you to use pessimistic locking (select for update) when you work with ActiveRecord models.

Pessimistic locking behavior for Yii2 ActiveRecord

  1. Installation
  2. Usage
  3. TODO
  4. Licence
  5. Links

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->lockForUpdate();
    $model->doSomethingWhileLocked();
    $dbTransaction->commit();
} catch(\Exception $e) {
    $dbTransaction->rollBack();
    throw $e;
}

TODO

Tables with compound primary keys are not supported.

Licence

MIT

Links

0 0
2 followers
0 downloads
Yii Version: 2.0
License: MIT
Category: Database
Developed by: Alexey Samoylov
Created on: Jul 16, 2015
Last updated: 2 years ago

Related Extensions