xakki/yii2-ar-json

ActiveRecord json field

Installs: 4

Dependents: 0

Suggesters: 0

Security: 0

Stars: 2

Watchers: 3

Forks: 0

Open Issues: 0

Type:yii2-extension

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

This package is auto-updated.

Last update: 2024-03-29 03:51:20 UTC


README

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);
    }
    
}