Five Reasons Why You Should Move To Yii2

Okay, there were a lot of questions about moving to ‘unstable’ version 2.

I give you five things I like the most.

  1. Code styling standards.

As for me, the old code styling was awful. In fact, there was no style at all (sorry guys).

New code has strict coding standards similar to Zend (?). And tabs. I like tabs.

  1. Php 5.4.

Runs faster, good syntax, many enhancements (see changelog). Actually, they had me at short arrays :)

And no, it’s not a drawback, it’s the reason you must come to your boss (or hosting company) and say “hey, gimme 5.4!”

  1. User identity and RBAC.

Nothing to say here, these two had just become much simpler.

  1. asArray()

Take a look at this:





    ->andWHere(['some_field' => 'some_value'])



So here I take my model, add some named scopes and other ActiveRecord (ActiveQuery actually) stuff, and then return all the records without constructing the AR objects. It saves memory, and also you can do something like…

  1. …response formatting.

You’ve probably noticed already that Yii2’s actions must return render result now.

And (besides other cool things) now you can do this:

$data = MyModel::find()

    ->asArray() // asks query to return array of data


\Yii::$app->response->format = 'json'; // how about xml?

return $data;

Isn’t it cool?

Moreover, response formats are extensible, so you can create your own, and then - boom! - response->format = ‘pdf’!

Well, there are many other things actually (like composer packages, new RAR, View class, et cetera et cetera) but you’ll definitely love these five.

Yes, and these are great :)

But for me the documentation is very less. And my main problem with the Yii, I couldn’t understand the philosophy of the Model classes.

How should I implement my Model class? What is the best practice?

In many forums I read that every second developers hate the Gii, they need better ORM.

Why is the SearchModel in seperate class? (and folder by the Gii)? Why the object fields in the SearchModel class? and why are not in the original Model class too? (only just in the comment).

However I don’t like the new AR, because some (in my friendship) developers use in the controller the AR, instead of in the model… They build the AR queries in the controller class. For me this gives a shit to the MVC modell. In my view.

And I experienced that the Yii insert 2x slowlier the models into the database than the Symfony2. However the read is more faster.

(InnoDB, mysql, same model…, and I didn’t use the validaton for models in Yii, only just in Symfony2, or the Laravel).

However the reading from the database is 3x faster than Symfony2 :). And the AR seems very fast if you need to fetch data from the database.

Okay, here’s a killer feature.

$query = MyModel::find()




    ->andWhere(['some_field' => 'some_value']);

echo $query->createCommand()->getRawSql(); // Such query. Much params bound. Very amaze.

And a usecase for that.

Basically, you can create ultrasophisticated queries using AQ, just don’t forget to quote and disambiguate.

And really many more to expect in Yii2 than the features you pointed out. A few of them discussed in an earlier post as well.


Everybody have his own list :)

Today my ‘favourite killer feature list’ was extended by Qiang’s last commit (support of subqueries).


 $query->select('*')->from(['u' => $subQuery]);

is totally awesome.

So it’s “seven reasons” now, not five.

I’ll wait until ten and modify the headline.

Yes the Query Builder has been created with some really… I mean really… good thought behind it. I can imagine how much it would make life really easy for developers.

A very simple use case of using the [font="Courier New"]SELECT … IN …[/font] clause to filter records using dropdowns becomes so easy…


    'status' => 10, // could be a variable for specific Yii::$app->user->id

    'id' => [4, 8, 15, 16, 23, 42], // an array from your multi select dropdown


replacement for…


SELECT * FROM tbl_name

WHERE (status = 10) AND (id IN (4, 8, 15, 16, 23, 42));


What about saving relational data?

Has this been integrated in yii2 or do we still need extensions/behaviors?


AFAIR there are some methods for that (link, unlink) but I’ve never used this stuff (in other frameworks also), I prefer to set FK manually.

Sorry, I’m a little bit old-fashioned, so I hate magic.

These are compelling reasons. My next project will definitely be Yii 2.

I’ve migrated a production application to Yii2 and had only two bugs, both already fixed in the framework itself. Nothing was missing until now, because most of the extensions I used are now a part of the core extensions, like Boostrap widgets and assets compression.

As long as I have a good test coverage, I feel safe enough to do this and keep updating Yii2. ;)

from a beginners point of view or one considering to move from another php framework (even yii 1.1), these reasons are weak.

That new array shorthand syntax…it’s making me leave php…

Kidding (but still find it confusing and ugly), i guess i’ll get used to it once i’ll start coding in Yii 2.0, like one year from now on or something like this since migrating huge projects from 1.x branch is not that easy.

Actually most of the languages has it already (in Perl and JS we have {} also, which is even more weird-looking), only PHP was using array().

You do not need to use it if you don’t want to :wink:

I agree. However, there’s no stopping progress so I’ll start with something trivial like my personal website :)

config/web.php :)

Sure, if you use the application templates you have to convert everything back yourself :stuck_out_tongue:

Well, just once. Then you can use your own "old-style-arrays-basic" template instead of official one.