Archive for the ‘php’ Category

Migrating to cakephp 2.0 tips and changes

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

# Rename all helpers in view directory to match for example country.php becomes CountryHelper.php.

3. Move AppController (former app_controller) from app/ directory to app/Controller/ directory.

Also capitalize all model class for example user.php becomes User.php

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()

NOTE: Cakephp 2.0 has some built in folders under Views like Scanffold, Elements, Emails etc similar to those if want to capitalize view folders for example dont make users => Users, then make sure that Controllers are same name $name = “Users” otherwise they will not work in unix boxes where folder path app/Views/users/ are treated differently than app/Views/Users.

# In Helpers

If you have custom helpers under “Views/Helpers” directory that loads other helpers for example $Html or $Form there will be small change in construct process for example $html = new HtmlHelper() becomes $html = new HtmlHelper($this->_View)

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”.
  • In controllers for example AppController Helper inclusion for view should be carefully stated for example var $helpers = array(‘ ‘Js’,  ’Html’,  ’Form) etc javascript Helper is now called Js helper and html is Html.’

# Authentication

  • Cake 1.3.x was automatically checking if user has correctly entered username/password inside your login() method of users_controller but in cake 2 we need to manually call $this->Auth->login() this returns boolean value based on successful login or failure.

# Saving forms

When submitting forms from views to controllers Model->save() method check for !empty($this->request->data) instead of !empty($this->data)

File uploads

  • When submitting multipart-form-data for attaching files from views to controllers, the file data cannot be accessed via $this->data variable rather they are stored into $this->params["data"]

6. Others

  • All component files should be extended by Component Class instead of Object

Some precautions

  1. Do not overwrite routes.php file under your app/Config folder.

Tips

You can use Dreamweaver’s excellent find and replace tool, it works for a single file or recursively on a entire directory. or Netbeans also has Edit -> Replace in project

Cakephp creating dynamic menus

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.

Cakephp created & modified fields update 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.