Giter VIP home page Giter VIP logo

yii2-readonly-behavior's Introduction

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.

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.

Installation

The preferred way to install this extension is through composer.

Either run

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

or add

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

to the require section of your composer.json file.

Usage

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

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.

yii2-readonly-behavior's People

Contributors

synatree avatar

Watchers

Nikolya avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.