PHP code example of laxity7 / yii2-json-field
1. Go to this page and download the library: Download laxity7/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/ */
laxity7 / yii2-json-field example snippets
/** @inheritdoc */
public function behaviors(): array
{
return [
[
'class' => \laxity7\yii2\behaviors\JsonFieldBehavior::class,
'fields' => ['foo_data', 'bar_data'],
],
];
}
use laxity7\yii2\behaviors\JsonFieldBehavior;
use yii\db\ActiveRecord;
/**
* @property int $id
* @property array $foo_data
* @property array $bar_data
*/
class Foo extends ActiveRecord {
/** @inheritdoc */
public function behaviors(): array
{
return [
[
'class' => JsonFieldBehavior::class,
'fields' => ['foo_data', 'bar_data'],
'jsonOptions' => JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE,
'skipEmpty' => false,
'defaultValue' => ['foo' => 'bar'],
'asArray' => true,
],
];
}
// Based on these parameters may be approximately the code
public function updateBar(int $id, array $barData): array
{
$model = self::findOne(['id' => $id]);
$model->foo_data['foo'] = 'bar1';
$model->bar_data['bar'] = array_merge($model->bar_data['bar'], $barData);
$model->save();
return $model->bar_data['bar'];
}
}