PHP code example of vitaly-alexandrovich / yii2-json-behavior
1. Go to this page and download the library: Download vitaly-alexandrovich/yii2-json-behavior 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/ */
vitaly-alexandrovich / yii2-json-behavior example snippets
// Создаем модель для данных
class UserContacts extends \yii\base\Model
{
public $phone;
public $email;
public $skype;
}
// В существующей ActiveRecord модели используем поведение JsonBehavior
class User extends \yii\db\ActiveRecord
{
public static function tableName()
{
return '{{%user}}';
}
public function behaviors()
{
return [
// Указываем колонку с данными и модель для них
\yii\behaviors\JsonBehavior::bind('contacts', UserContacts::class),
];
}
}
$user = User::findOne(1);
$user->contacts->phone;
$user->contacts->email;
$user->contacts->skype;
$user = User::findOne(1);
$user->contacts->phone = '+7 (111) 222-33-44';
$user->contacts->email = '[email protected]';
$user->contacts->skype = 'user';
$user->save();