Menus

Wednesday, 2 November 2016

Yii2 Usefull Tips



Filter to force lowercase or uppercase on all operations


Use this way
['username', 'filter', 'filter'=>'strtolower'],
Add this code  your model in rules


 public function rules()
    {
        return [
            [['item_code', 'item_name', 'amount','qty'], 'required'],
            [['opening_stock', 'stock','addstock','qty'], 'integer'],
            [['amount'], 'number'],
            [['item_code'], 'string', 'max' => 20],
            [['item_name'], 'string', 'max' => 150],
            [['item_name'], 'unique'],            
            [['item_name','item_code'], 'filter', 'filter'=>'strtoupper'],
        ];
    }


How to set timezone in yii


When someone is reffering to application property or application config it means "root" config variable, in this case timeZone.
Config part:
<?php
// in protected/config/main.php
return array(
    'timeZone' => 'Asia/Calcutta'
    // Other configuration....
);

Example

project_folder/common/config/main.php

<?php
return [
    'vendorPath' => dirname(dirname(__DIR__)) . '/vendor',
    'components' => [
        'cache' => [
            'class' => 'yii\caching\FileCache',
        ],
    ],
    'timeZone' => 'Asia/Calcutta', //Time zone set
];



Configure at common/config/main.php:
'components' => [
    ...
    'formatter' => [
        'class' => 'yii\i18n\Formatter',
        'dateFormat' => 'php:j M Y',
        'datetimeFormat' => 'php:j M Y H:i',
        'timeFormat' => 'php:H:i',
        'timeZone' => 'Europe/Berlin',
    ],




No comments:

Post a Comment