项目作者: amoydavid

项目描述 :
MySQL InnoDB pessimistic lock support(select for update) and SQL Server ROWLOCK support for Yii2 ActiveRecord
高级语言: PHP
项目地址: git://github.com/amoydavid/yii2lockable-query.git
创建时间: 2018-01-06T08:44:09Z
项目社区:https://github.com/amoydavid/yii2lockable-query

开源协议:MIT License

下载


Yii2 Lockable ActiveQuery

This package allows you to use pessimistic locking (select for update) when you work with ActiveRecord Query.

Installation

The preferred way to install this extension is through composer.

Either run

  1. php composer.phar require --prefer-dist amoydavid/yii2lockable-query "*"

or add

  1. "amoydavid/yii2lockable-query": "*"

to the require section of your composer.json.

Usage

Set schema map for database connection

  1. <?php
  2. return [
  3. 'class' => 'yii\db\Connection',
  4. 'schemaMap' => [
  5. 'mysql' => '\amoydavid\Yii2LockableQuery\mysql\Schema', // set up mysql schema
  6. 'mssql' => '\amoydavid\Yii2LockableQuery\mssql\Schema', // set up mssql schema
  7. ],
  8. 'dsn' => 'mysql:host=localhost;dbname=yii',
  9. 'username' => 'root',
  10. 'password' => '',
  11. 'charset' => 'utf8',
  12. // Schema cache options (for production environment)
  13. //'enableSchemaCache' => true,
  14. //'schemaCacheDuration' => 60,
  15. //'schemaCache' => 'cache',
  16. ];

Extend your ActiveRecord class via \amoydavid\Yii2LockableQuery\ActiveRecord

  1. /**
  2. * Class Sample
  3. * @package common\models
  4. *
  5. */
  6. class Sample extends \amoydavid\Yii2LockableQuery\ActiveRecord { ... }

Use model locks in transaction.

  1. $dbTransaction = $model->getDb()->beginTransaction();
  2. try {
  3. $model = Sample::find()->where(['id'=>1])->forUpdate()->one();
  4. $dbTransaction->commit();
  5. } catch(\Exception $e) {
  6. $dbTransaction->rollBack();
  7. throw $e;
  8. }

Licence

MIT