Posted on November 2nd, 2011 by admin | No Comments »
Cakephp has released 2.0 version with lot of changes in API, and nice features. The highly sought ORM capability is also added in this version, while I was migrating from my old 1.3.x app to cake 2.0 faced lots of dificulties and thought to share with you peoples. This is a quick note, more verbose information can be found at
http://book.cakephp.org/2.0/en/appendices/2-0-migration-guide.html
1. Rename all controllers pizza_controller.php becomes PizzaController.php same as classs name
2. Rename All components for example my_mail.php becomes MyMailComponent.php
3. Move AppController (former app_controller) from app/ directory to app/Controller/ directory.
4. In views
- Use $this->Html->link() instead of $html->link()
- Use $this->Form-> instead of $form->
- Use $this->Session-> instead of $session->
- Use $this->Paginator-> intead of $paginator ->
- For JavaScript inclusion use $this->Html->script(“”) instead of $javascript->link()
5. In Controller
- Using model find method $this->Student->find(array(“id” => 123)) will not work use find(“first”, array(“id” => 123)).
- If you have tried to modify $this->data like $this->data["xyz"] = “abc” inside controller method this will throw error [Indirect modification of overloaded property ... has no effect] instead you need to do $this->request->data["xyz"] = “abc”.
6. Others
- All component files should be extended by Component Class instead of Object
Some precautions
- Do not overwrite routes.php file under your app/Config folder.
Posted on October 21st, 2010 by admin | 1 Comment »
Generating categories or list of selectable items in cake sites are common task this can be done by pulling list of category data in your app controller and then using one view element so that they appear in every page. Here is the step by step process to do that. Add the following lines of code in your app_controller.php file.
$categories = ClassRegistry::init('Category')->find('all');
$this->set('categories', $categories);
Above lines of code can be put under beforeRender() function in app_controller.php I am assuming that categories is the name of table. Next thing is to create one element file in views/elements/ folder with any name say category.ctp now place the following code under that
<ul>
<?php foreach($categories as $key => $data) : ?>
<li><?php echo $data['Category']['name'] ?></li>
<?php endforeach; ?>
<ul>
The last thing to actually show this output into your your website you need to edit current layout file (default.ctp etc) and include your element that created in previous step. just all the following line of code in your layout file
<?php echo $this->element('category'); ?>
Thats all… Post a comment here if you still face any problem.
Posted on September 15th, 2010 by admin | No Comments »
This was the problem I had two columns in my table “created” and “modified’ both were timestamp and when did insert both took the value of current timestamps till here everything was okay but when I did any UPDATE using cakephp save method both the fields were updated again with the new timestamps values whereas I wanted only “modified” field to be changed. I was knowing that this is something that cake handles automatically so I went through manuals in search of more information and found that cakephp will update both upon new insert and for each updates it will only change modified. So I thought there might be some bug in cakephp or some tweak is necessary that I can do in models like using beforeSave() method etc. But no help after wasting hours..!
Later I went to my phpmyadmin and had a chance to look at table structure and was stumbled to see one attribute on the side of “created/modified” columns “on update CURRENT_TIMESTAMP”. From that instant it was clear to me that all problems were arising due to this line and cakephp has nothing to do with it. with little timepass I was able to get rid of that line doing editing table structure etc. There are some restriction on mysql tables that there cannot be two timstamps etc without above line. I am sure you can figure out how to fix that. So the lesson learn from this case was default generated “created/modified” column names always carry “on update CURRENT_TIMESTAMP” attribute and we need to remove them.

