Yii2 join condition when you use viaTable

Sometimes you need add specific condition in query to via table in yii2.
Example, you need to get categories where clients with active subscribe status(subscribed == true) only:

//standard using viatable without condition(s)
 public function getCategories()
        return $this->hasMany(Categories::className(), ['id' => 'category_id'])
            ->viaTable('categories_xref', ['client_id' => 'id']);

Solution with condition in yii2 via table query:

 public function getCategories()
        return $this->hasMany(Categories::className(), ['id' => 'category_id'])
            ->viaTable('categories_xref', ['client_id' => 'id'], function ($query) {
                $query->onCondition(['subscribed' => true]);

Easy. Happy codding!

Relation “migration” already exists error while executing yii migrate

If you use PostgreSQL Database and got error
“Relation “migration” already exists error while executing yii migrate”
Just check your config, it seems that you have a mistake in schema section.

'components' => [
        'db' => [
            'class' => 'yii\db\Connection',
            'dsn' => 'pgsql:host=localhost;dbname=yourdb',
            'username' => 'yourdb_user',
            'password' => 'password',
            'charset' => 'utf8',
            'schemaMap' => [
                'pgsql' => [
                    'class' => 'yii\db\pgsql\Schema',
                    'defaultSchema' => 'public'
            ], // PostgreSQL
        'cache' => [
            'class' => 'yii\caching\FileCache',

Work with ZIP-archives in PHP 5.2+

Starting with PHP 5.2 you can use useful library ZipArchive for easy working with ZIP archives.
(If your PHP version les 5.2 you’ll get error: class ziparchive not found)

Steps for easy working with ZipArchive

$zip = new ZipArchive;

Read ZIP archive:


Create new ZIP archive (just add ZipArchive::CREATE):

$zip->open('{file_location_path}', ZipArchive::CREATE);

Example of extracting archive on your server:

$zip = new ZipArchive;
if ($zip->open('{file_location_path}') === true){
	echo 'Archive file is not found';

Example of creating archive on your server:
Continue reading Work with ZIP-archives in PHP 5.2+

What difference between extension, zend_extension, zend_extension_ts in PHP.ini (extension VS zend_extension)?

If you see in php.ini something like:

zend_extension = php_xdebug-2.5.1-7.1-vc14.dll
;zend_extension_ts = php_xdebug-2.0.4.dll ;for PHP<=5.2

extension – it’s native PHP extension
zend_extension – it’s Zend extension for PHP(mainly using for profiling/debug).
Both extension kinds share lots of stuff. The difference between both types is mainly in hooks they register into the Engine.
Remember that, despite it is very uncommon, an extension can be both a PHP extension and a Zend extension at the same time. Xdebug is a good example.

zend_extension_ts – it’s old(for PHP <5.3) threads safe Zend extension (not actual)
Detailed scheme of loading extension and zend_extension:

More detailed info you can read here: https://wiki.php.net/internals/extensions

NS_ERROR_FILE_CORRUPTED How to fix? in Firefox console.

If you catch in Firefox console NS_ERROR_FILE_CORRUPTED then you have problems with local storage 🙁 Bad news
Good news. Fast fix:
0. Close firefox and go to


1. remove(or rename) webappsstore.sqlite
2. Run Firefox.

Happy browsing)

Install Xdebug on Windows (PHP)

Simple steps for install XDebug on your Windows machine.
0. Write on test.php this code:


1. Copy all informmation from phpinfo page.
2. Open https://xdebug.org/wizard.php and insert phpinfo to textarea
3. Click “Analyse my phpinfo() output” and follow to provided instructions
Something like:

  • Download php_xdebug-2.5.1-7.1-vc14.dll
  • Move the downloaded file to E:\xampp\php\ext
  • Update E:\xampp\php\php.ini and change the line zend_extension = E:\xampp\php\ext\php_xdebug-2.5.1-7.1-vc14.dll
  • Restart the webserver
$obj1 = new stdClass();
$obj1->name = 'A';
$obj1->date = date('Y-m-d H:i:s', 1458754362);

Happy debugging.

Yii requirements.php error after moving to /web folder.

If you get:


The path to yii framework seems to be incorrect.

You need to install Yii framework via composer or adjust the framework path in file requirements.php.

Please refer to the README on how to install Yii.

Warning: require_once({your_site}/web/vendor/yiisoft/yii2/requirements/YiiRequirementChecker.php): failed to open stream: No such file or directory in {your_site}\web\requirements.php on line 23

Fatal error: require_once(): Failed opening required '{your_site}/web/vendor/yiisoft/yii2/requirements/YiiRequirementChecker.php' (include_path='...php\PEAR') in {your_site}\web\requirements.php on line 23

Then just change 14 line on:

$frameworkPath = dirname(__FILE__) . '/../vendor/yiisoft/yii2';

Happy codding.

Fast installing Yii framework

0. If you do not already have Composer installed, you may do so by following the instructions at getcomposer.org.
1. Install Yii)

composer global require "fxp/composer-asset-plugin:^1.2.0"

and final

composer create-project --prefer-dist yiisoft/yii2-app-basic my_project_name

That’s all. Happy Codding.
Continue reading Fast installing Yii framework

Show/hide data lines and columns in google chart (remove column onclick/toogle multiple checkboxes/labels)

How to show and hide lines data in standart google chart?
Example of chart build:

google.charts.load('current', {'packages': ['corechart']});
function drawChart() {
    var data = google.visualization.arrayToDataTable([
        ['Year', 'Sales', 'Expenses'],
        ['2013', 1000, 400],
        ['2014', 1170, 460],
        ['2015', 660, 1120],
        ['2016', 1030, 540]

    var columns = [];
    var series = {};
    for (var i = 0; i < data.getNumberOfColumns(); i++) {
        if (i > 0) {
            series[i - 1] = {};

    var options = {
        title: 'Chart title',
        vAxis: {minValue: 0},
        tooltip: {trigger: 'selection'},
        aggregationTarget: 'category',
        series: series

    var chart = new google.visualization.AreaChart(document.getElementById('chart_div'));
    chart.draw(data, options);

    google.visualization.events.addListener(chart, 'select', function () {
        var sel = chart.getSelection();
        if (sel.length > 0) {
            if (sel[0].row === null) {
                var col = sel[0].column;
                if (columns[col] == col) {
                    columns[col] = {
                        label: data.getColumnLabel(col),
                        type: data.getColumnType(col),
                        calc: function () {
                            return null;
                    series[col - 1].color = '#CCCCCC';
                else {
                    columns[col] = col;
                    series[col - 1].color = null;
                var view = new google.visualization.DataView(data);
                chart.draw(view, options);

If you catch: php5-imap requires ext-imap * -> the requested PHP extension imap is missing from your system.

If you see: “The requested PHP extension imap is missing from your system.”
It seems that you need to install imap extension on your system.
The following simple steps below:

apt-get install php5-imap

However, it’s not enabled IMAP by default so enable it with:

php5enmod imap

Then bounce apache:

service apache2 restart

If you got “IMAP extension must be enabled.” in PHP.
Continue reading If you catch: php5-imap requires ext-imap * -> the requested PHP extension imap is missing from your system.

PHP. Save information into text/log file. Bacis examples.

Simple example for save information to file on server by PHP

$content = '[' . date('Y-m-d H:i:s') . ']: test info log' . PHP_EOL . '------------' . PHP_EOL;
$fp = fopen($_SERVER['DOCUMENT_ROOT'] . "/save_test.log", "a+");
fwrite($fp, $content);

Error log using example(logs $content [array|string|object] to the log file):

$obj = new StdClass();
$content = [$obj, [3, 51, '4k']];
error_log('[' . date('Y-m-d H:i:s') . ']:' . PHP_EOL . print_r($content, TRUE) . PHP_EOL, 3, $_SERVER['DOCUMENT_ROOT'] . "/save_test.log");

Also you can use file_put_contents method:
Continue reading PHP. Save information into text/log file. Bacis examples.

Native PHP Sort array of objects by property

Usually, it is very often necessary to sort the data in an array. But when in the array we have objects it happened little bit difficult.
Fast solution for sorting array objects by property name.
Bacis solution (PHP 5.3+) :

function sortArrayWithObjects($array, $property)
    usort($array, function ($a, $b) use ($property) {
        return (($a->$property == $b->$property) ? 0 : (($a->$property < $b->$property) ? -1 : 1));
        //[short version] return strcmp($a->$property, $b->$property);
    return $array;

$obj1 = new stdClass();
$obj2 = clone $obj1;
$obj3 = clone $obj2;
$obj1->name = 'B';
$obj2->name = 'C';
$obj3->name = 'A';
$data = [$obj1, $obj2, $obj3];
$result = sortArrayWithObjects($data, 'name');
echo '<pre>';
echo '</pre>';

Continue reading Native PHP Sort array of objects by property