My findings in Yii and comparisons to CakePHP

December 20th, 2008

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 lazy loading (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.

Excellent Documentation
And you can find that out for yourself.

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.

Faster
Benchmarks.

Integration with jQuery
Cake uses prototype. Booo!

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:

public function rules() {
	return array(
		array('password', 'authenticatePass'),
		//more rules...
	);
}

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!

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • Technorati
  • Reddit
  • RSS
  • Twitter

Categories: CakePHP, Yii

Leave a comment

Comments Feed26 Comments

  1. Daniel H.

    Hi Jonah, I’m currently using Cake for some projects and don’t really have the time to try Yii … but please keep posting about your experience with it … I’ll be listening :)

  2. Nik

    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

    Anyway, thanks for the information it’s nice review.

  3. Brian D.

    I second Daniel. This framework sounds very interesting. I appreciate you sharing your experiences with it and hope that you continue to do so.

  4. zf

    I really wish CakePHP would dump PHP4 and focus on PHP5. It’s time to modernize that framework I think. Great coders, and great ideas – only too little php5 stuff

  5. Jonah

    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.

  6. Yii and CakePHP > Cake Toppings | Tips & tricks about CakePHP by you

    […] 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 […]

  7. Nik

    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. :)

  8. Jonah

    @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.

  9. Cassiano Surek

    Thanks for the analysis. I am trying to assess whether to use Yii or Kohana for a new project and this has given me some valuable information about Yii. Cheers.

  10. mrLami

    “Cake uses prototype. Booo!”

    Very Funny…

  11. Jackson Gabbard

    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.

  12. Jonah

    Thanks for the comments guys.

    @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).

  13. Mark De Verno

    I enjoyed the comparison of Yii to Cakephp. Thank you.

  14. Code Sucker

    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.

  15. Colleen

    yah Cakes clinging to php4 really holds it back IMHO.

  16. Richard@Home

    Very useful comparison. Thanks for posting!

    Bookmarked for future updates :-)

  17. arun

    I think Symfony is the best framework…

  18. Megabt

    arun ?
    I think Symfony is the best framework…
    why?

    the crud on symfony is better, in yii is a baby…
    but the core and OO programing mode gain my atenttion and now I am trying yii

    regards

  19. John

    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.

  20. Kshitij

    Thanks for the update. Have been looking for it on the net for long and found one more article worth a read. May be it can help future visitors:

    http://daniel.carrera.bz/2009/01/comparison-of-php-frameworks-part-i/

  21. gayanhewas

    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

  22. Jonah

    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

  23. Dustin

    I chuckled when you mentioned “beautiful code”. Since switching to Python/Django, PHP looks nothing but ugly to me.

  24. Jonah

    I’ve touched Django also, and I can’t disagree. Still, I think Yii does a great job at making php more beautiful.

  25. Mohamed Cherif BOUCHELAGHEM

    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.

  26. Toby

    If PHP has to be used, Yii is the best choice – also as far as development costs is concerned.

    Some shortcomings of PHP are however:

    – less development tools (e.g. profiling in IDE, less debugging features)

    – no Hibernate support

    – once in a while exploits are revealed you need to stuff

    – no “singleton” patterns

    For anyone with some Java experience and time, I would recommend to have a look at Tapestry5 (Apache project) as a possible alternative. Especially something like the IOC service is really neat!

Leave a comment


6 − five =

Feed

http://php-thoughts.cubedwater.com / My findings in Yii and comparisons to CakePHP