skobka/yii2-json-field

Help you to define fields, that can contains json. Json in this fields would be automatically serialized and deserialized

Maintainers

Package info

github.com/Doka-NT/yii2-json-field

Type:yii2-extension

pkg:composer/skobka/yii2-json-field

Statistics

Installs: 2 387

Dependents: 0

Suggesters: 0

Stars: 3

Open Issues: 0

v1.0.3 2017-02-04 19:27 UTC

This package is not auto-updated.

Last update: 2026-03-01 04:09:26 UTC


README

Help you to define fields, that can contains json. Json in this fields would be automatically serialized and deserialized

Installation

The preferred way to install this extension is through composer.

Either run

php composer.phar require skobka/yii2-json-field

or add

"skobka/yii2-json-field": "*"

to the require section of your composer.json file.

Usage

Once the extension is installed, simply use it in your code by :

### Product.php

/**
 * @property object|array|null $field1  
 */
class Product extends AvtiveRecord {
   use JsonFieldTrait;
   
   public function behaviors()
   {
        return [
            'field1' => [
                'class' => JsonFieldBehavior::class,
                'dataField' => 'json_field_1', // this is the name of field in db table
            ],
        ];
   }
}

### ProductController.php
// saving 
$product = Product::findOne(['id' => 1]);

$product->field1 = new \StdClass();
$product->field1->foo = 'bar';
$product->save();

$product = Product::findOne(['id' => 1]);
print $product->field1->foo; // bar