xakki / yii2-ar-json

ActiveRecord json字段

安装: 4

依赖: 0

建议者: 0

安全: 0

星星: 2

关注者: 3

分支: 0

开放问题: 0

类型:yii2-extension

v0.2 2019-02-05 01:51 UTC

This package is auto-updated.

Last update: 2024-08-29 05:18:27 UTC


README

将一些字段合并到json字段ActiveRecord

composer require xakki/yii2-ar-json

此表仅包含字段 - idcreateoption。其中option是一个json字段(对于PostgreSQL是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);
    }
    
}