i-excellent/yii2-pgsql-schema

Yii2 的 PostgreSQL 架构,支持 jsonb 和 json 字段

安装: 28

依赖: 0

建议者: 0

安全: 0

星星: 1

关注者: 2

分支: 0

开放问题: 0

类型:yii2-extension

1.0.2 2017-12-17 16:38 UTC

This package is not auto-updated.

Last update: 2024-09-29 05:01:40 UTC


README

改进的 Yii2 PostgreSQL 架构。

支持以下 ActiveRecord 模型类型

Latest Stable Version License

限制

当您使用此扩展时,您不能使用数组指定 PDO 类型:[值, 类型],

例如 ['name' => 'John', 'profile' => [$profile, \PDO::PARAM_LOB]]

参见问题 #7481

安装

安装此扩展的首选方式是通过 composer

运行以下命令之一

php composer.phar require --prefer-dist i-excellent/yii2-pgsql-schema

或者

"i-excellent/yii2-pgsql-schema": "~1.0"

将以下内容添加到您的 composer.json 文件的 require 部分。

配置

一旦扩展安装完成,请将以下代码添加到您的应用程序配置中

return [
    //...
    'components' => [
        'db' => [
            'class' => 'yii\db\Connection',
            'dsn' => 'pgsql:host=localhost;dbname=<database>',
            'username' => 'postgres',
            'password' => '<password>',
            'schemaMap' => [
                'pgsql'=> 'excellent\pgsql\Schema',
            ],
        ],
    ],
];

配置模型规则

/**
 * @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'],
        ];
    }
}

用法

您可以将数组、json 和时间戳类型以如下方式保存到数据库中

/**
 * @var ActiveRecord $model
 */
$model->attribute1 = ['some', 'values', 'of', 'array'];
$model->attribute2 = ['some' => 'values', 'of' => 'array'];
$model->save();

然后可以在您的代码中使用它们

/**
 * @var ActiveRecord $model
 */
$model = Model::findOne($pk);
$model->attribute1; // is array
$model->attribute2; // is associative array (decoded json)

复合类型

许可证

MIT