Since I have found out about Yii I have been using it to work on a “dummy” website with features that are generally used across a large variety of websites (e.g. news system, user system, authorization, etc). This is also exactly what I did with CakePHP when I first started learning it, and the reason I do this is not only so I have a set of re-usable resources but also to help learn the framework. Although I still consider myself a novice in Yii I have learned a lot of things about it, some things very similar to CakePHP while other things very different. It’s a learning experience and I must say Yii so far seems a better alternative to CakePHP for many reasons. I will outline a few reasons/differences here.

PHP 5 features included in the core
First, and most importantly, what allows for most of the stuff that I love in Yii over CakePHP is that it is PHP 5 only. PHP 4 is not supported. Because of this, Yii is able to take use of many PHP 5 features that Cake is not able to. Mainly, that would be the PHP 5 magic overloading functions, which leads to the next point:

OOP style AR and lazy loading
Yii takes use of PHP 5’s overloading functions in its AR implantation in a very beautiful manner. Instead of storing results from quarries in an array as Cake does, in Yii you can access the quarries in an OOP manner. Here is an simple example of accessing AR in Yii, taken out of the Yii documentation.

//Database scheme is 'post' BELONGS_TO 'user'. Each row in the 'posts' table has a corresponding row in the 'users' table.// retrieve the post whose ID is 10$post= Post::model()->findByPk(10);// retrieve the post's author: a relational query will be performed here$user=$post->user;

Note the last line. What actually happens is that the related user data is loaded dynamically by simply calling the variable. The reason for this is because Yii takes use of lazyloading (and of course PHP 5’s magic). This means the data is not retrieved until it is actually needed, increasing performance and ease of programming. If lazy loading does not fit you, you can use eager loading too, which will work similar to Cake’s ContainableBehavior. Example:

$posts= Post::model()->with('user')->findByPk(10);

Now that is what I call beautiful code. You can probably tell what the above does implicitly, but if you can’t, it simply pulls the post with id of 10 along with its corresponding user (a mysql JOIN will be used). Beautiful? I think yes.

Fast and useful developer feedback
Along with the documentation the Yii forum is also an excellent place to get help. I have posted a few questions/bug reports there and have gotten very fast feedback, from the lead developer himself if from no one else. The bug reports and enchantments I requested there [1, 2, 3] were all fixed/implanted within 24 hours of reporting. Therefore I give Yii two thumbs up for developer feedback. Arguably the user feedback is not as good has Cake’s user feedback however, as Cake is much more well known and more widely in use (Yii only just came out with a stable version I believe this month). But developer feedback is more valuable than user feedback in my opinion.

More easily extended
One example I have for this so far and that I have tested are the validation rules. Not only can you define a action in the model as a validation as in Cake, but you can can define a whole class to be a validation type. For instance, you could create a authenticatePass class that works as a validator that you can easily drop into your projects. Then you could use it in your model as so:

The above validator could be designed to validate the password to the username (if sent) in the users table.

Regarding the design pattern
As far as the structure, e.g. the MVC design pattern, Yii and CakePHP are very similar. They both have “components” for example. Yii also has “widgets”, which can represent anything from a form calendar to a navigational menu. Widgets consist of both components and views, so in a way, they are similar to Cake’s “plugins” I suppose, but they are not so intimidating to create. Yii also has their own version of the Bakery for sharing extensions.

Since this post is quite long enough, I will stop here. In future articles I will try to focus on more narrow topics as I don’t feel this post is nearly long enough to satisfy my given subject line. Oh well. Hope you learned something useful!

Probably if you need to start with framework Yii is a good alternative, but having experience with a Framework have also advantages, such as knowing the syntax which is not small at all rather to study the new one :)

Also benchmarks are not so correct especially with Cake, because they comparing Cake 1.1

Thanks guys for the comments. I was really not expecting to get this much attention on only my second article for this blog!

@Daniel
I certainly will :)

@Nik
Didn’t understand your first sentence but yes, in the benchmark they use an older and supposedly slower version of Cake. But I don’t think the difference is more than 10%. I know when I develop in Yii it certainly feels faster.

@Brian
Thanks :) and again, I certainly will continue to do so.

@zf (zend framework?)
I totally agree. The coders are excellent and their blogs are fun to read. But I wonder if they dump PHP 4 and add PHP 5 features, whether it will be as optimized as a framework that was made for PHP 5 features from the start? Also, it doesn’t seem like that they will dump PHP 4 anytime soon. They say they will dump it for CakePHP 2.0, but that is a long way off.

[…] I stumbled against a blog post by Jonah while writing about the Yii framework in comparisment to CakePHP. I liked what I read but always have to try it out myself. So I will do that in the future. Maybe […]

Jonah, I didn’t understand my first sentence while I am reading it now either – hahaha :)

I wanted to say, that when you stick to a framework, no matter what it is, you have advantage that you know the syntax and common usage of the methods which is small. I believe that I can start easily to develop on symfony but it will take some time to understand and to remember the syntax of it. :)

@Nik
Definitely. For instance the advantage for me using CakePHP right now is that I know it really well and so can develop fast with it. However, as I play with Yii hopefully I will get to know it just as well as cake and my hypothesis is that it will be even faster and better to program with.

On the note of jQuery/Prototype being supported by Yii and Cake respectively, I just wanted to throw in that I have rarely found that I can make fast use of framework-integrated JS stuff. I’m a front-end developer, so I do a lot of complex JS/AJAX-based enhancements. Of course I use the framework to supply data and handle AJAX requests from the front-end, but the JS is always independent of the framework. For that matter, I’ve rarely found HTML helpers to be cleaner than just writing the HTML though.

@Jackson Gabbard
I totally agree there. I don’t use the integrated JS stuff either. What is nice about it though is that jquery is packed with the framework, so you don’t have to download it separately. Also you can take use of CClientScript::registerCoreScript() to register jquery core files (&of course it will make sure it is only registered once).

What else comes to mind, but it is not complete:
1.) prepared statements – yii has it, cake not (I may be wrong)
2.) RBAC ind YII vs. ACL in cake. If you compare AUTH in both frameworks you will see that cake really is a mess and so badly implemented that developers themselves are not able to use it in a secure way – see the not-so-long-ago security hole (this was a big one!).

of course everybody here must go and compare to rails or django to understand, what a framework nowadays REALLY can do for you – yii is still miles away from these.

Hi I’ve built quite a few sites, some of them pretty big with Cake – and its a great framework. Recently though I’ve experimenting with Yii. You are right, there is a lot to love about it – although I haven’t built any real sites with it yet, I would certainly recommend people give it a whirl.

Hi,
I am trying to adopt a Framework to work on my final year project. But i have no experience in using a framework at all. I am confident in php5 and mvc sine i use the design patter with my projects. what would you guys recommend to use as a Framework which has good support and shorter learning curve.
Thanks
Gayan

So your looking for a framework with the very shortest learning curve? What pops up in my head is codeigniter. I tried it once, and if I remember correctly it was pretty simple and easy to learn, although I would say less robust and powerful as Yii

Thank you for sharing Jonah, I had a similar experience as you with CakePhp and Yii but I was waiting until Yii be more mature, now i am switching to Yii, you are totaly right in your arguments but i think you miss to mention the consequences of using Php4 in CakePhp and the disadvantage of using it, cause it’s really very slow if we compared to Yii that’s the cost of not using the lazy loading.