PHP code example of yiimaker / yii2-data-static

1. Go to this page and download the library: Download yiimaker/yii2-data-static library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

yiimaker / yii2-data-static example snippets


class AboutUs extends ymaker\data\statics\StaticData
{
    public $phone;
    public $email;

    public function rules()
    {
        return [
            [['phone', 'email'], '

$aboutUs = new AboutUs();

$aboutUs->phone = '+111111111111';
$aboutUs->email = '[email protected]';
$aboutUs->save();

$aboutUs->loadAttributes();
echo $aboutUs->email; // '[email protected]';

$aboutUs = AboutUs::getInstance();

$aboutUs->loadAttributes();
$aboutUs->email = '[email protected]';
$aboutUs->reload();

echo $aboutUs->email; // '[email protected]';

class AboutUs extends ymaker\data\statics\StaticDataTranslation
{
    public $address;

    public function rules()
    {
        return [
            [['address'], '

$aboutUs = new AboutUs(['language' => 'en-US']);
// $about

$aboutUs->address = 'Kiev, Ukraine';
$aboutUs->save();
$aboutUs->setLanguage('ru-RU');
$aboutUs->address = 'Киев, Украина';
$aboutUs->save();

$aboutUs->loadAttributes();
echo $aboutUs->address; // 'Киев, Украина'
$aboutUs->changeLanguage('en-US');
echo $aboutUs->address; // 'Kiev, Ukraine'

$aboutUs = AboutUs::getInstance(['language' => 'en-US']);

$aboutUs->loadAttributes();
$aboutUs->address = 'Лондон, Великобритания';
$aboutUs->reload();

echo $aboutUs->address; // 'Киев, Украина'

    /**
     * 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);

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; // 'Киев, Украина'
bash
php composer.phar