2017 © Pedro PelĂĄez
 

library yii2-json-object-behavior

image

supplyhog/yii2-json-object-behavior

  • Friday, February 26, 2016
  • by devsupplyhog
  • Repository
  • 2 Watchers
  • 4 Stars
  • 4,927 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 2 Forks
  • 0 Open issues
  • 1 Versions
  • 3 % Grown

The README.md

yii2-json-object-behavior

Installation

Install this extension via composer. Add this line to your project’s composer.json, (*1)

supplyhog/yii2-json-object-behavior” : “dev-master”

What Does It Do?

JSON string field in the database becomes usable object or array of objects on an yii\db\ActiveRecord. No need to covert it back to a string for the database on saving. Just use it like a property., (*2)

Behavior Config

public function behaviors()
{
  return [
    //It is best to name your behaviors so that you can use more than one
    'field_behavior' => [
      'class' => 'supplyhog\JsonObjectBehavior\JsonObjectBehavior',
      'attribute' => 'field', //Name of the attribute that holds the JSON string
      'objectClass' => JsonObjectModel::className(), //Replace with your own class that extends JsonObjectModel
      'init' => true, // (new Model())->field will be an object
      // 'default' => [], // If you need defaults 
    ],
    'field_array_behavior' => [
      'class' => 'supplyhog\JsonObjectBehavior\JsonObjectBehavior',
      'attribute' => 'fieldArray', //Name of the attribute that holds the JSON string
      'array' => true, //This is an array of objects
      'init' => true, // (new Model())->fieldArray will be an array
      'default' => [ //Object defaults. Useful sometimes. The field value
        'class' => JsonObjectModel::className(),  //Replace with your own class that extends JsonObjectModel
        'hello' => 'world', // the attribute hello will be set to "world" if the JSON string does not have a value for it 
      ],
    ],
  ];
  }

JsonObjectModel

This is provided for a base for your JSON strings to change into. It extends \yii\base\Model which is quite helpful. It is not required, but if you use your own make sure you look at the magic __toString() in the JsonObjectModel and copy it over to yours., (*3)

The Versions

26/02 2016

dev-master

9999999-dev

  Sources   Download

BSD-2-Clause

The Requires

 

The Development Requires

by Ethan Rogers
by Wil Wade