Giter VIP home page Giter VIP logo

yii2-json-behavior's Introduction

Yii2 json attribute behavior

Auto decode/encode attribute value in json, provide array access and json validator.

WARNING! From version 2.0.14 Yii has built-in DB JSON-type, and this behavior is no longer required.

Russian readme

Packagist Version Code Coverage Scrutinizer Build Status Total Downloads

Install

Install via Composer:

composer require paulzi/yii2-json-behavior

or add

"paulzi/yii2-json-behavior" : "~1.0.0"

to the require section of your composer.json file.

Usage

JsonBehavior

Configure your model:

use paulzi\jsonBehavior\JsonBehavior;

class Item extends \yii\db\ActiveRecord
{
    public function behaviors() {
        return [
            [
                'class' => JsonBehavior::className(),
                'attributes' => ['params'],
            ],
        ];
    }
}

Now you can access to attribute as array:

$item = Item::findOne(1);
$item->params['one'] = 'two';
$item->params['two'] = [];
$item->params['two']['key'] = true;
$item->save();

$item = Item::findOne(1);
echo $item['two']['key']; // true

Set attribute via json string:

$item = new Item();
$item->params->set('[2, 4, 42]');
echo $item->params[2]; // 42

Set attribute via array:

$item = new Item();
$item->params->set(['test' => ['one' => 1]]);
echo $item->params['test']['one']; // 1

Convert to json string:

$item = new Item();
$item->params['test'] = ['one' => false, 'two' => [1, 2, 3]];
var_dump((string)$item->params); // {"one":false,"two":[1,2,3]}

Convert to array:

$item = new Item();
$item->params->set('{ "one": 1, "two": null, "three": false, "four": "four" }');
var_dump($item->params->toArray());

Check empty:

$item = new Item();
$item->params->set('{}');
var_dump($item->params->isEmpty()); // true

emptyValue

You can set emptyValue option to define an empty JSON value (default null). Can be '{}', '[]'' or null.

JsonValidator

Configure your model (see behavior config upper):

use paulzi\jsonBehavior\JsonValidator;

class Item extends \yii\db\ActiveRecord
{
    public function rules() {
        return [
            [['params'], JsonValidator::className()],
        ];
    }
}

Validate:

$item = new Item();
$item->attributes = ['params' => '{ test: }'];
var_dump($item->save()); // false
var_dump($item->errors); // ['params' => ['Value is not valid JSON or scalar']]

You can set merge = true, in this case, instead of replacing all field data of the transmitted data, array_merge() will be applied with old data in the field (which are taken from oldAttributes of ActiveRecord). This option can be apply only with ActiveRecord:

use paulzi\jsonBehavior\JsonValidator;

class Item extends \yii\db\ActiveRecord
{
    public function rules() {
        return [
            [['params'], JsonValidator::className(), 'merge' => true],
        ];
    }
}

JsonField

You can use JsonField class for other models:

class Item
{
    public $params;
    
    public function __constructor()
    {
        $this->params = new JsonField();
    }
}

// ...

$item = new Item();
$item->params['one'] = 1;
var_dump((string)$item->params); // {"one":1}

How To

Usage isAttributeChanged() and getDirtyAttributes()

Yii2 does not provide the ability to inject code to check attribute dirty.

If you need to use methods isAttributeChanged() or getDirtyAttributes(), you can override them in model:

/**
 * @inheritdoc
 */
public function isAttributeChanged($name, $identical = true)
{
    if ($this->$name instanceof JsonField) {
        return (string)$this->$name !== $this->getOldAttribute($name);
    } else {
        return parent::isAttributeChanged($name, $identical);
    }
}

/**
 * @inheritdoc
 */
public function getDirtyAttributes($names = null)
{
    $result = [];
    $data = parent::getDirtyAttributes($names);
    foreach ($data as $name => $value) {
        if ($value instanceof JsonField) {
            if ((string)$value !== $this->getOldAttribute($name)) {
                $result[$name] = $value;
            }
        } else {
            $result[$name] = $value;
        }
    }
    return $result;
}

yii2-json-behavior's People

Contributors

paulzi avatar ptheofan 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.