yii2-pgsql
Improved PostgreSQL schemas for Yii2., (*1)
Supports follow types for ActiveRecord models:, (*2)
, (*3)
Limitation
When you use this extension you can't specify the PDO type by using an array: [value, type],, (*4)
e.g. ['name' => 'John', 'profile' => [$profile, \PDO::PARAM_LOB]]., (*5)
See the issue #7481, (*6)
Installation
The preferred way to install this extension is through composer., (*7)
Either run, (*8)
php composer.phar require --prefer-dist i-excellent/yii2-pgsql-schema
or add, (*9)
"i-excellent/yii2-pgsql-schema": "~1.0"
to the require section of your composer.json file., (*10)
Configuration
Once the extension is installed, add following code to your application configuration:, (*11)
return [
    //...
    'components' => [
        'db' => [
            'class' => 'yii\db\Connection',
            'dsn' => 'pgsql:host=localhost;dbname=<database>',
            'username' => 'postgres',
            'password' => '<password>',
            'schemaMap' => [
                'pgsql'=> 'excellent\pgsql\Schema',
            ],
        ],
    ],
];
Configure Model's rules, (*12)
/**
 * @property string[] $attribute1 array of string
 * @property array $attribute2 associative array or just array
 * @property integer|string|\DateTime $attribute3 for more information about the type see \Yii::$app->formatter->asDatetime()
 */
class Model extends ActiveRecord
{
    //...
    public function rules()
    {
        return [
            [['attribute1'], 'each', 'rule' => ['string']],
            [['attribute2'], 'safe'],
        ];
    }
}
Usage
You can then save array, json and timestamp types in database as follows:, (*13)
/**
 * @var ActiveRecord $model
 */
$model->attribute1 = ['some', 'values', 'of', 'array'];
$model->attribute2 = ['some' => 'values', 'of' => 'array'];
$model->save();
and then use them in your code, (*14)
/**
 * @var ActiveRecord $model
 */
$model = Model::findOne($pk);
$model->attribute1; // is array
$model->attribute2; // is associative array (decoded json)
Composite types, (*15)
License
MIT, (*16)