https://github.com/yiimaker/yii2-data-static
It is a model for the data that stores configuration.
https://github.com/yiimaker/yii2-data-static
model static yii2-extension
Last synced: 3 months ago
JSON representation
It is a model for the data that stores configuration.
- Host: GitHub
- URL: https://github.com/yiimaker/yii2-data-static
- Owner: yiimaker
- License: bsd-3-clause
- Created: 2017-01-03T16:33:12.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2017-01-10T15:03:53.000Z (over 8 years ago)
- Last Synced: 2025-01-04T20:44:14.005Z (5 months ago)
- Topics: model, static, yii2-extension
- Language: PHP
- Size: 24.4 KB
- Stars: 3
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
Yii2 Static Data
================
It is a model for the data that stores configuration.
`StaticData` subject to the same rules as `yii\base\Model`Installation
------------The preferred way to install this extension is through [composer](http://getcomposer.org/download/).
Either run
```bash
php composer.phar require --prefer-dist yiimaker/yii2-data-static "*"
```or add
``` json
"yiimaker/yii2-data-static": "*"
```to the require section of your `composer.json` file.
Usage
-----
1. Configure component `yiimaker/yii2-configuration` in config file or in `StaticData` class. [More information](https://github.com/yiimaker/yii2-configuration#configuration)
2. Inherit the class `ymaker\data\statics\StaticData`, then describe it as a normal model.Example
-------StaticData
----------
```php
class AboutUs extends ymaker\data\statics\StaticData
{
public $phone;
public $email;public function rules()
{
return [
[['phone', 'email'], 'required'],
['phone', 'string', 'max' => 255],
['email', 'email']
];
}
}
``````php
$aboutUs = new AboutUs();
```
#### Save Data```php
$aboutUs->phone = '+111111111111';
$aboutUs->email = '[email protected]';
$aboutUs->save();
```#### Load Data
```php
$aboutUs->loadAttributes();
echo $aboutUs->email; // '[email protected]';
```
or```php
$aboutUs = AboutUs::getInstance();
```#### Reload Data
```php
$aboutUs->loadAttributes();
$aboutUs->email = '[email protected]';
$aboutUs->reload();echo $aboutUs->email; // '[email protected]';
```StaticDataTranslation
---------------------
```php
class AboutUs extends ymaker\data\statics\StaticDataTranslation
{
public $address;public function rules()
{
return [
[['address'], 'required'],
['address', 'string', 'max' => 255],
];
}
}
``````php
$aboutUs = new AboutUs(['language' => 'en-US']);
// $about
```
#### Save Data```php
$aboutUs->address = 'Kiev, Ukraine';
$aboutUs->save();
$aboutUs->setLanguage('ru-RU');
$aboutUs->address = 'Киев, Украина';
$aboutUs->save();
```#### Load Data
```php
$aboutUs->loadAttributes();
echo $aboutUs->address; // 'Киев, Украина'
$aboutUs->changeLanguage('en-US');
echo $aboutUs->address; // 'Kiev, Ukraine'
```
or```php
$aboutUs = AboutUs::getInstance(['language' => 'en-US']);
```#### Reload Data
```php
$aboutUs->loadAttributes();
$aboutUs->address = 'Лондон, Великобритания';
$aboutUs->reload();echo $aboutUs->address; // 'Киев, Украина'
```
#### change language```php
/**
* change language for model
* @param $language string language code
* @param bool $reload If true, then all attributes will be overwritten
*/
public function changeLanguage($language, $reload = true);
``````php
echo $aboutUs->address; // 'Киев, Украина'
$aboutUs->changeLanguage('en-US');
echo $aboutUs->address; // 'Kiev, Ukraine'$aboutUs->changeLanguage('ru-RU', false);
echo $aboutUs->address; // 'Kiev, Ukraine'$aboutUs->reload();
echo $aboutUs->address; // 'Киев, Украина'
```