https://github.com/i-excellent/yii2-pgsql-schema
Improved PostgreSQL schemas for Yii2.
https://github.com/i-excellent/yii2-pgsql-schema
extension jsonb postgresql postgresql-database schema yii2
Last synced: 2 months ago
JSON representation
Improved PostgreSQL schemas for Yii2.
- Host: GitHub
- URL: https://github.com/i-excellent/yii2-pgsql-schema
- Owner: i-excellent
- License: mit
- Created: 2017-12-17T15:55:49.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2017-12-17T19:00:23.000Z (over 8 years ago)
- Last Synced: 2025-03-30T02:15:46.009Z (about 1 year ago)
- Topics: extension, jsonb, postgresql, postgresql-database, schema, yii2
- Language: PHP
- Homepage:
- Size: 16.6 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
yii2-pgsql
==============
Improved PostgreSQL schemas for Yii2.
Supports follow types for ActiveRecord models:
[](https://packagist.org/packages/i-excellent/yii2-pgsql-schema)
[](https://packagist.org/packages/i-excellent/yii2-pgsql-schema)
Limitation
------------
When you use this extension you can't specify the PDO type by using an array: `[value, type]`,
e.g. `['name' => 'John', 'profile' => [$profile, \PDO::PARAM_LOB]]`.
See the issue [#7481](https://github.com/yiisoft/yii2/issues/7481)
Installation
------------
The preferred way to install this extension is through [composer](http://getcomposer.org/download/).
Either run
```
php composer.phar require --prefer-dist i-excellent/yii2-pgsql-schema
```
or add
```
"i-excellent/yii2-pgsql-schema": "~1.0"
```
to the require section of your `composer.json` file.
Configuration
-------------
Once the extension is installed, add following code to your application configuration:
```php
return [
//...
'components' => [
'db' => [
'class' => 'yii\db\Connection',
'dsn' => 'pgsql:host=localhost;dbname=',
'username' => 'postgres',
'password' => '',
'schemaMap' => [
'pgsql'=> 'excellent\pgsql\Schema',
],
],
],
];
```
Configure Model's rules
```php
/**
* @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:
```php
/**
* @var ActiveRecord $model
*/
$model->attribute1 = ['some', 'values', 'of', 'array'];
$model->attribute2 = ['some' => 'values', 'of' => 'array'];
$model->save();
```
and then use them in your code
```php
/**
* @var ActiveRecord $model
*/
$model = Model::findOne($pk);
$model->attribute1; // is array
$model->attribute2; // is associative array (decoded json)
```
[Composite types](docs/composite.md)
License
-------
[MIT](LICENSE)