PHP code example of skobka / yii2-json-field
1. Go to this page and download the library: Download skobka/yii2-json-field library. Choose the download type require. 2. Extract the ZIP file and open the index.php. 3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
skobka / yii2-json-field example snippets
### 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
php composer.phar