Tips for developers coming from CakePHP?

I’m looking at the possibility of converting two CakePHP projects (one is a my own personal project that is already completed and the other is a client’s website that is a WIP) to Yii. I’ve looked at the blog and some of the other tutorials in the Documentation section of this site, as well as the blog screencast (which seems to really whizz through things too fast for a Yii newbie IMHO), but I’m still left scratching my head since it’s quite a departure from what I’m used to in CakePHP and there’s so many classes and such to deal with now in Yii.

Is there anyone else who was/is in a similar situation that can offer some tips or tricks when converting CakePHP projects, or maybe at the very least some kind of analogous comparisons regarding the similar parts of both frameworks (i.e. the CakePHP Auth component is the equivalent of x and/or y in Yii)?

I wrote this a while back:

http://php-thoughts.cubedwater.com/2008/my-findings-in-yii-and-comparisons-to-cakephp/

I agree the number of classes can seem a bit overwhelming, and many extend off of many others, so figuring out what you need to know to fulfill a task can be daunting at first. Don’t let it stop you though, as Yii is very powerful, and the code is very nicely written. The forums are a bit slow at times as there don’t seem to be enough experienced users posting to help out the newbies (at least not in the first 24 hours of a new topic), but we’re trying to get things hopping a bit more :slight_smile:

Also check out IRC. Things were a bit slow there, but in the past week it’s picked up nicely :smiley:

I think the only really notable point (that was important to me) in comparison between Cake & Yii, is that Cake picks up model/db relations by convention, whereas Yii has no database conventions. Yii does indeed pick up relations, but you must be using foreign key constraints for that to work.

I don’t like the fact that there are no endorsed db conventions to follow (as that results in contributed code varying wildly in db structure), but once I picked up the obscure tip about foreign keys, I was happy.

Good luck!

EDIT: btw, looking at the actual classes directly, has really helped me more than the docs, as the docs can be vague in some spots (i.e. - a class may take an array as it’s configuration, but there aren’t always definitions of what is possible in that array), so checking the code helps with that alot.

I am curious, why did you go from CakePHP to Yii? Is Yii better? My first framework experience is with Yii, but I know that Zend is the most popular one out there.

Well, one of the main reasons is that Yii is PHP5-only and thus doesn’t have to include tricks which may impede performance in order to support PHP4 (AFAIK CakePHP 1.3 still has support for PHP4). Also, Yii has a minimum requirement of PHP 5.1.0, whereas the forthcoming PHP5-only version of CakePHP (2.0) is going needs at least PHP 5.2.0, something my client doesn’t have on their systems out in the field (they run 5.1.6).

I also haven’t been very impressed with the lack of response on a ticket regarding a fairly trivial, yet non-minor problem, that I submitted to the CakePHP bug tracker some time ago.

Don’t get me wrong though, CakePHP does have a fair number of advantages IMHO, but I’m just curious about Yii and any/all performance and other benefits it may have, being PHP5-only (and other things).

The one problem I’m just now finding out from reading the documentation and forum posts is that one of the highlights of Yii is AR, which apparently can severely (?) decrease performance when handling a lot of records due to the high number of instantiations that have to occur (one instance per record). I thought this might be overcome somehow with the use of APC or similar, but it’s not clear that is the case. So now I’m wondering if switching to Yii really would be better for this particular project, which handles at least hundreds of thousands of records in a single table.

Well I don’t think it’s a problem when you have a big table since you most likely don’t fetch all rows (AR’s) at once. If you need something like that (eg for sitemap creation in a cronjob or something) you can still use DAO.

Yii uses PHP 5 conventions, it doesn’t have a split development team (CakePHP just went thru a huge mess), and it has tons of functionality that you’d have to add in to CakePHP.

I only wish Yii had a more active community, but it’ll get there.

The AR issue isn’t specific to Yii, it’s pretty much any system that implements ORM or AR, will see decreased performance due to unoptimized queries.

mscdex, AR is not a reason to be concerned about your switch though, as you can use Yii’s query builder or direct db access to query on your own w/out using AR, and those alternative options are the same other frameworks would offer. My personal recommendation would be to use AR by default on tables that aren’t used heavily (i.e. - a supporting table of data) just so you can still get your code tossed together quickly, and for heavy-use tables, resort to building your own queries (assuming you have experience with building optimal sql queries ;) )

EDIT:

I just remembered that you would have an issue w/ performance, using AR, if you additionally display many records on one page, each having related models/tables, if you allow AR to use it’s default lazy loading approach, as then any related data that you reference, would generate a query per relation per row.

So keep in mind w/ AR to be a bit more optimal, use "eager loading", aka the ->with() param.

I don’t mind writing queries myself, but I guess I’m spoiled by CakePHP in that it allows queries to by dynamically generated by way of PHP-based drivers (since CakePHP doesn’t use PDO unless an individual driver chooses to do so) and the results that are returned simply consist of an associative array containing the records from that query. This helps to abstract DB-specific quirks and/or syntactical issues that exist and thus makes the code less-reliant on how the underlying relational database works.

Ideally it would be nice if there was a way to have Yii do this too, instead of having to strictly choose between AR or writing entire query strings. I’m thinking of something along the lines of the same kind of parameters AR uses for its findAll* methods (i.e. arrays containing conditions, attributes, etc), but instead of returning an array of AR objects, it could return an associative array similar to result of calling CDbDataReader’s fetchAll().

I’m not sure how this might be implemented, but I think it would be very useful for queries that are not overly complex that need optimizing over and above the queries Yii can generate. In this case then it makes sense to allow someone to write out the entire database query via CDbConnection’s createCommand method.

I think what Yii does is actually better than what you are describing @mscdex

To use active record, you do not need to know sql. Take this example:


$users = User::model()->findAllByAttributes(array('firstName'=>'John'));

The above will find all users with the first name "John"

In CakePHP, when you did a query like this it would return the results in a plain multi-dimensional array, simply containing attribute=>value pairs

Yii will go a step further and return an array of AR objects, each AR object representing a row in the database.

The only reason I can think of that AR would be bad is if you need to query a heck of a lot of data, and you are worrying about running out of memory. I have run into this case for generating statistics.

In that case, you could do something like the follow:




$criteria = new CDbCriteria;

$criteria->condition = 'firstName=:name';

$criteria->params = array(

	'name'=>$_GET['name'],

);


$dataReader= Yii::app()->db->commandBuilder->createFindCommand('user', $criteria)->query();



This creates essentially the same query as the first example

See this:

http://www.yiiframework.com/doc/guide/database.dao#executing-sql-statements

That’s a nice solution.

mscdex, another option is to implement a 3rd party query builder if you’d like to have an alternative to Yii’s DAO and AR. You could likely even implement cake’s methods, hehe.

I understand how the AR find methods work and I agree they’re great for small amounts of data.

That’s just it, generating statistics and other data-intensive queries on entire tables is one of the main features for my client, so having a non-AR method is critical in this situation.

Regarding the last line of code there, that seems awfully more wordy (IMHO) than something like:


User::model()->findAllByAttributes(array('firstName'=>'John'));

I just wish there was a similar concise method (like AR’s findAllByAttributes for example) to retrieve a plain array of results containing only primitives and not objects.

Why not just create a variable that encapsulates the commandbuilder command?




$model = Yii::app()->db->commandBuilder;

$result = $model->createFindCommand('user', $criteria)->query();



Additionally, I haven’t looked into it, but you could possibly override some of the AR methods to control how the data is returned. So instead of an object-per-record, create an array, while still having the easy search functionality.

This might be interesting to have:




CActiveRecord::createFindCommand($criteria) {

     return Yii::app()->db->commandBuilder->createFindCommand($this->tableName(), $criteria);

}


$result = Model::model()->createFindCommand($criteria)->queryRow();



Something like this might be even nicer:




$results = Model::model()->commandBuilder->with(..)->createFindCommand($criteria)->queryRow();



the command builder would basically be like this:

http://www.yiiframework.com/doc/api/CDbCommandBuilder

Only joins would be pulled from relations() if needed and the $table arguments would obviously not be needed for the methods

I sent this thread to one of my developers who is familiar with CakePHP and has just begun to work with Yii. We only adopted Yii recently and for most of the team its not only that Yii is new to them but its their first PHP framework.

Here is the developer in questions reply:

"Cake has an easier and less memory intensive way of returning data, but has backwards support for PHP4 that creates an unnecessary overhead.

I find Cake’s way of accessing and presenting data a lot easier to work with, but that is minor. Cake’s error reporting from the application is more extensive and makes troubleshooting a lot easier.

I have gotten quite comfortable with Yii, creating forms, queries, data presentation, etc but I haven’t seen a benefit in Yii over Cake yet. Everytyhing available in Yii is available in Cake, it just comes down to understanding how each framework is structured and the conventions that they are expecting."

I don’t want to make this a Cake versus Yii discussion but if people are looking for tips working with Yii it seems that having used a framework before makes the transition to Yii a lot easier.

I would definitely say anybody coming back to PHP from a RoR environment would definitely be happier and more comfortable with Yii over Cake. But I don’t like Cake anyway 8)

I’m not sure how old this post is, I just joined and did a search for CakePHP. I too am coming from a cakePHP background, where I had a lot of issues learning but I am quite proficient with now, a year later. I found Cakes default containable behavior to be an issue for me, the application I was working on was not originally developed by me, and used containable for every find. I must say up until about 5 months ago I too would have used it because I never read where it should and shouldn’t be used. So why am I looking at Yii?

I just finished the CakePHP project and know there is another project coming in the next few weeks. This new project will need something to be built on, sure I could do it all from scratch but I like frameworks, they save a lot of coding. So I first looked at CakePHP again. The entire development team seems to be up on how it works and what each should edit, but the bloated search results would need to be managed, the queries IMHO leave a lot to be desired. For example continually writing my where clause in an array format took some getting use to, but when you have to do a join, union or even a where x and y or z and a style query the array structure is not intuitive at all. I just looked at CakePHP 2.0 whited moved to PHP 5.2.x as mentioned above, which we do not have an issue with, however having an app in production on 1.3.7 it would not make sense to have yet another on 2.0 with a huge project of upgrading the older version to the newest release. The production project could benefit from a lightweight framework, so if I move to Yii for the new project and it works as I think it will, then moving the current production app to Yii would be more worth the time than converting it from cakePHP 1.3.7 to cakePHP 2.0, maybe 2.5 by then.

I’m excited to see how Yii handles these things. I’m currently reading through the Getting Started documentation and I’m up to Components. I really liked the first part where right away it creates a site, however it mentions that you should store the Yii framework outside of your webdocs but doesn’t explain how. It turned out to be a simple thing to do, but for clarity it would be nice to see that added to the document as a note. I’m still wrapping my head around where to place bits of code and where a team might divide the talents, Front-end developers work in directories x,y and z backend developers work in directories a,b and c. Right now in the examples and looking at the simple site started with the documentation I am not seeing much in the way of front-end directories, but I’m still reading so maybe it will show itself. Anyway, I wanted to comment because the first post had a short comparison and I found that quite informative, as for these other solutions, I’m not there yet, hopefully soon.