Xakki / yii2-ar-json

Combine some field into json field ActiveRecord

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

One field for all

Combine some field into json field ActiveRecord

composer require xakki/yii2-ar-json

This table has only field - id, create, option The option is a json filed (for PostgreSQL is a jsonb)

/**
 * Example class
 * @property integer $id
 * @property string $create
 * @property string $text
 * @property array $data
 */
class Example extends \xakki\yii2-ar-json\components\ActiveRecordJson
{
    protected $_json_attribute = 'option';
    protected $_json_catch = ['text', 'data', 'info'];

    public static function tableName()
    {
        return '{{example}}';
    }
    
    /**
    * For new record only
    */
    public function setAttributeDefault() {
        $this->text = 'Example text';
        $this->data = ['test'];
        $this->info = 2
    }
    
    public static function doSomeThing() {
        $thisData = self::findOne(1);
        echo $thisData->text;
        print_r($thisData->data);
    }
    
}

About

Combine some field into json field ActiveRecord


Languages

Language:PHP 100.0%