https://github.com/locustv2/yii2-json-data-behavior
Enables you to easily encode and decode json in your activerecords
https://github.com/locustv2/yii2-json-data-behavior
Last synced: over 1 year ago
JSON representation
Enables you to easily encode and decode json in your activerecords
- Host: GitHub
- URL: https://github.com/locustv2/yii2-json-data-behavior
- Owner: Locustv2
- License: bsd-3-clause
- Created: 2017-03-31T17:56:49.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2017-03-31T19:16:55.000Z (about 9 years ago)
- Last Synced: 2025-02-24T16:54:42.171Z (over 1 year ago)
- Language: PHP
- Size: 3.91 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# JsonData behavior for Yii2 ActiveRecords
Enables you to easily encode and decode json in your activerecords. It automatically encodes before validating and saving to database and decodes after.
[](https://packagist.org/packages/Locustv2/yii2-json-data-behavior)
[](https://packagist.org/packages/Locustv2/yii2-json-data-behavior)
[](https://packagist.org/packages/Locustv2/yii2-json-data-behavior)
[](https://packagist.org/packages/Locustv2/yii2-json-data-behavior)
## Installation
The preferred way to install the library is through [composer](https://getcomposer.org/download/).
Either run
```
php composer.phar require --prefer-dist locustv2/yii2-json-data-behavior
```
or add
```json
{
"require": {
"locustv2/yii2-json-data-behavior": "~1.0.0"
}
}
```
to your `composer.json` file.
## Usage
```php
public function behaviors()
{
return [
'class' => \locustv2\behaviors\JsonDataBehavior::className(),
'dataAttribute' => 'hotel_data',
];
}
```
After configuring your activerecord as above, you can use as follows:
```php
$model = Hotel::find()->one();
var_dump($model->getData('rooms')); // assume it returns a list of rooms
var_dump($model->getData('rooms.0.price')); // to get rooms data
$model->setData('ratings', [
'5star' => ['count' => 100],
'4star' => ['count' => 200],
'3star' => ['count' => 20],
'2star' => ['count' => 75],
'1star' => ['count' => 50],
]);
var_dump($model->getData('ratings.3star.count')); // returns 20
```
## To do
- Add unit tests
## Contributing
Feel free to send pull requests.
## License
For license information check the [LICENSE](LICENSE.md)-file.