Saturday, January 30, 2010

How to Generate Options for HTML Select Tags in CakePHP

How to generate options for HTML Select Tags in CakePHP?

As such

  1. You can retrieve options from a Model (Database Table).
  2. Or, you can specify options for select tag in view files.
Just keep in mind CakePHP does not support ENUM type field. So, you may not use 'ENUM' datatype in table fields.

To retrieve options from Database Table

Say, you have two Models (tables) - Order and Size; 

Fields in 'sizes' table:
id, size
And fields in 'orders' table:
id, quantity, size_id

In your OrdersController file (/app/controllers/orders_controller.php)
$sizes = $this->Order->Size->find('all');
$this->set('sizes',$sizes);

Now in your view file: (/app/views/orders/add.ctp)

echo $form->create('Order');
echo $form->input('quantity');
echo $form->input('size_id');
echo $form->end('Submit');

And done.

Note the use of variable names. 
It is important to name the variables as per this convention. It will display all sizes available in database when you are adding a new order. To retrieve size field in table 'sizes', you need to specify variable name 'sizes' (note plural form) in your respective controller/action.  Now when you refer to 'size_id' in view file - cake will create a select tag on the fly with options retrieved from database.  

OK. It was pretty simple.

Now, how to display options for HTML Select Tag without any database table:

In this case, we have, say, only 'orders' table with following fields
id, quantity, size

In your view file (/app/views/orders/add.ctp), specify - 

$form->create('Order');
$form->input('quantity');
$sizes = array('s'=>'Small', 'm'=>'Medium', 'l'=>'Large');
echo $form->input('size', array('options'=>$sizes, 'default'=>'m'));
$form->end('submit');

That's it.

Friday, January 29, 2010

Managing Plugin URL in CakePHP

Just now I noticed my urls are getting deformed when I tried to access a plugin controller/action in my brand new CakePHP app. It took me sometime to understand that the problem was due to plugin call. When you are calling plugin controller/action - the url structure is:

 echo $html->link('Plugin Controller/Action Link', array('plugin' => 'plugin_name', 'controller' => 'controller_name', 'action' => 'action_name'));

The output will be:

http://example.com/plugin_name/controller_name/action_name

Make sure to mention 'plugin' => null in remaining urls, if you do not want to show plugin_name in other displayed links.

 echo $html->link('Non Plugin Link', array('plugin' => null, 'controller' => 'controller_name', 'action' => 'action_name'));

Output:

http://example.com/controller_name/action_name

If you do not specify 'plugin' => null, all the displayed links will show:

http://example.com/plugin_name/controller_name_1/action_name_1
http://example.com/plugin_name/controller_name_2/action_name_2
http://example.com/plugin_name/controller_name_3/action_name_3


From this what I get is that it is better to specify 'plugin' => null in case of all non-plugin links which are likely to be displayed in tandem with plugin links.



Or, there may be a short route - who knows?

Cake rocks... still learning each day...

Wednesday, January 27, 2010

How to Pass all URL Arguments to Paginator Functions

Use the following statement in view file for respective controllers/action:

$paginator->options(array('url' => $this->passedArgs));

This will retrieve all passed url arguments.

To specify which params to pass, use:

$paginator->options(array('url' =>  array("0", "1")));

Simple.

Thursday, January 14, 2010

CakePHP Pagination Change Label

Here we can change the displayed label for CakePHP Pagination:

Say, we want to sort results by user_id.

This is how it is done by default:

<?php echo $paginator->sort('user_id');?>


And to change the displayed label:
 <?php echo $paginator->sort('User', 'user_id');?>

Here User is the label which will be displayed.
user_id is the field to be sorted.