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.

Framework discoverability

I answered a question on http://programmers.stackexchange.com recently and I’d like to expand a little on my answer here.

Up until mid 2010 I used my own framework for all my freelance and personal development work, it had served me admirably over the years and in fact to this day it is still being used by one of my previous employers. I was part way through rewriting it to serve me for another few years.

Even though I had plenty of experience with other frameworks, Zend, Yii etc I still tended to use my own because it was so rapid and had never got in my way or not been fit for purpose.

One of the main reasons behind the full rewrite was Dlayer, my framework/library was very suited to building websites/shops etc but it wasn’t really suitable for Dlayer, Dlayer doesn’t really have a structure similar to traditional sites or apps.

I was rebuilding some of the core classes one evening and said to myself “what are you doing Dlayer isn’t going to use this class for months, if ever.”

I decided to prioritise, rather than rebuild my framework I was going to use an existing framework and build Dlayer, I was effectively just ‘wasting’ time.

As I stated at the start this came up because I answered a question on http://programmers.stackexchange.com about Zend being difficult and it got me thinking.

When I started using the Zend Framework the biggest issue for me was lack of discoverability, far too many of the methods/classes rely on data array to set their options. You end up spending ages either trawling through the code or even worse the online manual attempting to work out what keys you need in the array. The Yii framework takes this concept to the extreme, almost everything is driven by magic arrays.

There is nothing wrong with named params and setter methods, in fact from my point of view if a class needs a large array to set it up something is wrong.

Of all the options out there I still think the Zend Framework is generally the best fit, for me anyway but there is quite a steep learning curve.