Migrating your cakePHP 1.1 application to 1.2 - Chapter 2 Model changes
Please note that this article is intended for audiences that already have a decent understanding of the cakePHP frame work and essentially know where to look to make the necessary changes to the web application that you have already coded for the 1.1 framework.
Ok so now you have the default cakePHP page up and working, now for the fun part, moving the actual code over and seeing what works. Most applications use just the controllers,models, views, and webroot directories under the app directory, and there could be some things in the vendors directory as well. It will vary from application to application. Start by bringing the inocuous stuff forward first I would start with the webroot and vendors directories. Once you have proven that doesn’t cause any major problems, other than maybe losing your css formatting, then move onto the models,views,and controllers directories. At this point you have probably migrated all of your code over and have noticed what is working and more to the point what is not. At this point it is probably a good idea to turn the debugging level up to at least 1, but I would probably set it to 2 so you can get all the SQL data that the application is passing back and forth. This way you can see if the model find and save functions are working properly.
For the most part you will probably not have to change anything in the model definitions themselves. the bulk of the changes will come in the controllers that use the models to find/save data. Here are the major changes that I have found in the model methods that will require recoding.
1. findAll and findAll(By <Camel case table column id>) - these functions have been deprecated (removed), in version 1.2. This means that you need to replace all instances of these function calls within your code. In this case you need to use the new find function that is more specific in its use but is more flexible.
(i.e.) $this->Model->findAll($conditions,$fields,$order,$limit,$page,$recursive);
would become
$this->Model->find(’all’,array(’conditions’=>array(), ‘fields’=>array(), ‘order’=>array(), ‘limit’=>array(), ‘page’=>number, ‘recursive’=>number));
As you can see it is considerably longer to type, but is more specific in how you use it.
2. findCount and findCountBy(Camel case by table column) - this has also been deprecated (removed), the good news here is that the changes almost identical to the findAll with just one minor exception.
So $this->Model->findCount($conditions,$fields,$order,$limit,$page,$recursive);
would become
$this->Model->find(’count’,array(’conditions’=>array(), ‘fields’=>array(), ‘order’=>array(), ‘limit’=>array(), ‘page’=>number, ‘recursive’=>number));
Notice the only real change to the find command is the first word in the find function from ‘all’ to ‘count’. The nice thing about the newer version of findCount is that it just returns the numeric count instead of an array that you must index to get to the count.
Note the original find function has not really changed but under the hood in the cake libraries are basically doing this.
$this->Model->find(’first’,array(’conditions’=>array(), ‘fields’=>array(), ‘order’=>array(), ‘limit’=>array(), ‘page’=>number, ‘recursive’=>number));
So in essence the the find function has been re-written in cake 1.2 to be more specific in it’s use, the good news is that for the most part the new find functions appear to be more optimized and the generated SQL is considerably more compact than previous versions and seems to run quicker. There are four basic keywords for the new find function (all,count,first,list), where the keyword versions of ‘all’ and ‘count’ must be used for their deprecated counterparts. The ‘first’ keyword version can be used interchangeably with the standard find function. The last keyword ‘list’ is used to replace the generateList function which has also been deprecated, This was used primarily in forms to generate quick list of information for drop downs, however it could also be used for quick id and field lists. The function call looks just like the new declarations above, but first keyword changes as before. (i.e.)
$this->Model->find(’list’,array(’conditions’=>array(), ‘fields’=>array(), ‘order’=>array(), ‘limit’=>array(), ‘page’=>number, ‘recursive’=>number));
That about covers the major changes to the model class in cakePHP 1.2, this will keep anyone busy for some time changing out their controllers, and possibly helpers if there is model code in them. For the most part the model declarations in cakePHP 1.2 are basically the same as in 1.1, however if you are doing any special validation rules in your models, it would be a good idea to test those out once you get the controllers functioning properly. In the next chapter I will discuss the changes to the helper functions, and some of the rules enforcement changes on helpers as well.
Tags: Cake Development, CakePHP, CakePHP Documentation, CakePHP Help, CakePHP Migrations, PHPCake