2017 © Pedro Peláez
 

yii2-extension yii2-readonly-behavior

This behavior class allows you to specify an attribute which, upon setting or being set, prevents the model from being changed. Think of it as a latch that locks the model once a particular value has been set and saved.

image

synatree/yii2-readonly-behavior

This behavior class allows you to specify an attribute which, upon setting or being set, prevents the model from being changed. Think of it as a latch that locks the model once a particular value has been set and saved.

  • Monday, February 23, 2015
  • by synatree
  • Repository
  • 1 Watchers
  • 0 Stars
  • 74 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 2 Forks
  • 2 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

Read Only Behavior for ActiveRecord Models

This behavior class allows you to specify an attribute which, upon setting or being set, prevents the model from being changed. Think of it as a latch that locks the model once a particular value has been set and saved., (*1)

This is intended to act as a failsafe to prevent a fixed record from being updated even if you make a mistake in the controller and allow a change to be made., (*2)

Installation

The preferred way to install this extension is through composer., (*3)

Either run, (*4)

php composer.phar require synatree/yii2-readonly-behavior "*"

or add, (*5)

"synatree/yii2-readonly-behavior": "*"

to the require section of your composer.json file., (*6)

Usage

Once the extension is installed, simply use it in your code by adding the behavior to your ActiveRecord model:, (*7)

use synatree\behaviors\ReadOnlyBehavior;
...
public function behaviors(){
    return [
        'readonly' => [
            'class' => ReadonlyBehavior::className(),
            'attribute' => 'setmeonce',
            'onError' => function($param, $value){
                Yii::warning("Already Set, cannot set $param");
                throw new \yii\web\UnauthorizedHttpException("Already Set");
            }
        ]
    ];
}

Options

    'attribute' => 'modelattr', // your model attribute that will trigger a lock.  Lock will take effect when the record is loaded from or saved to the database.
    'onError' => ... // callable, either a function, function name, etc. per PHP manual is_callable
    'stopEvent' => true, // stop the AR event that tried to change an attribute.  If false, the db record will be updated, but the values will be the same as when locked.

The Versions

23/02 2015

dev-master

9999999-dev

This behavior class allows you to specify an attribute which, upon setting or being set, prevents the model from being changed. Think of it as a latch that locks the model once a particular value has been set and saved.

  Sources   Download

GPL-3.0+

The Requires

 

by David Baltusavich

extension yii2 behavior activerecord read-only