Yii, Gii and Zend Framework

In my current contract it has become common for me to have to switch multiple times a day between using the Yii Framework and the Zend Framework. In switching multiple times a day the differences between the two frameworks obviously jump out at me.

I’m not going to attempt to list or explain all the differences between the two frameworks or how each may be better given specific criteria but I will talk about one difference that I think contributes to lower quality apps when using Yii.

My opinions are based entirely on my professional history with Yii, given a different history I may have different opinions however I do feel that my opinions have logical merit.

I was hired to develop a backend for an existing web site that was becoming very time consuming to manage; I opted to use the Zend Framework and my default app setup.

An app for a completely different project was started about the same time as mine and for that the Yii Framework was chosen.

I am solely responsible for the design and development of the Zend Framework app, with the Yii app I am an additional developer, I just develop whatever is asked of me in a consistent manner to what has been created before.

Unlike Zend, Yii gives you a good starting point when you create a new web app, you get basic authentication, gii, a code generation system, loads of useful widgets and a working mini site. With Zend you get two controllers that don’t really do anything and you need to create everything. Earlier I mentioned my default app setup, rather than starting from a default Zend site I use my own default set up, it is a very simple Zend app that includes authentication, managed access control, audit logging and a few other core requirements.

The two applications are relatively similar in terms of complexity. Although similar in complexity they differ completely in one aspect, that being the quality and number of the models in the app.

Yii includes a module called Gii that in my opinion fools people into using it because of the perceived speed. Gii can inspect a table in your database, create a model with default validation based on the field data types and options and even create a default CRUD controller. The results are almost immediate; you get a sortable, searchable CRUD controller for the table/model.

This is where I believe the issue lies, Gii is capable of looking at foreign keys and creating the AR relations for you but it is still very much based around one model per table. Following this process it isn’t long before you end up with a model for every table and a CRUD controller per table. In all the cases I have seen the belief has always been it won’t take long to modify the generated CRUD controllers/views and add the additional functionality that may be required, however, that doesn’t happen, no one wants to develop code they don’t feel they need to.

This leads me to the difference between the two apps, the number of models, although both have a similar table count, 50 ish, the Zend app has 15-20 models, the Yii app has 50+.

Access to manage app content should in my opinion be very controlled, when managing content in my Zend app a single form may actually save the generated data/logic to multiple tables and access to that data may be controlled by several other tables, the one to one relation of Gii wouldn’t work.

I tend to end up with what is commonly referred to as fat models, my models contains lots of business logic and I don’t typically have a model per table, I have models which encompass an item/object that may in fact be represented by several tables.

Assuming you are consistent with the development of your app, something you should strive for, starting to use Gii can result in an app with lots of very simple models that just manage access to a single table, the result being that you end up with lots of disconnected business logic, split across models, in the controllers and even in the view with poor validation that isn’t necessarily tied into the business logic, just the field data type.

You can add content to the generated controllers / models but you have to be careful if you modify your database, we are developing after all. Gii will overwrite any changes you make if you re-run it, this isn’t technically a problem with version control but at this point you have lost the speed. If Yii created two model / controller classes, a non-modifiable one and a user one which extends them things would be better but the default problem still exists.

I realise that this isn’t necessarily a Yii framework issue it is just that in my opinion this is how Yii apps tend to end up. Not all Yii apps mind you, at Ladbrokes I worked on a Yii app which expanded upon Yii’s widget system and the result was a very powerful app.