I wanted some auto populating Select boxes in a site I was creating so that when I changed a Category the Subcategories would automatically update. CakePHP can do this pretty easily but it is let down by the documentation as there are no examples.
Initially I came up with a version that wrote JSON into a JavaScript variable in the page and then used jQuery to achieve the updating of select elements but I wanted to do this the Cake way which uses AJAX and as little code as possible.
Here is a simplified solution to demonstrate how it can be done.
This assumes a new CakePHP site already configured with a database connection. I used CakePHP 2.0.3 but this may also work with 1.3 (?)
1. Models
We need 3 associated tables
CREATE TABLE `categories` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(60) DEFAULT NULL,
PRIMARY KEY (`id`)
);
INSERT INTO `categories` (`id`, `name`)
VALUES
(1,'books'),
(2,'music'),
(3,'electronics');
CREATE TABLE `posts` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`title` varchar(50) DEFAULT NULL,
`subcategory_id` int(10) unsigned DEFAULT NULL,
PRIMARY KEY (`id`)
);
INSERT INTO `posts` (`id`, `title`, `subcategory_id`)
VALUES
(1,'The title',1),
(2,'A title once again',4),
(3,'Title strikes back',7);
CREATE TABLE `subcategories` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`category_id` int(10) unsigned DEFAULT NULL,
`name` varchar(60) DEFAULT NULL,
PRIMARY KEY (`id`)
);
INSERT INTO `subcategories` (`id`, `category_id`, `name`)
VALUES
(1,1,'fiction'),
(2,1,'biography'),
(3,1,'children'),
(4,2,'classical'),
(5,2,'rock'),
(6,2,'jazz'),
(7,3,'camera'),
(8,3,'audio'),
(9,3,'tv');
Bake the 3 Models for these and allow CakePHP to define model associations for you.
2. Controllers
Bake a Controller for the Post Model with the default CRUD actions.
While you are at it you will also need to bake the CRUD Views for the Post Controller.
If you browse to the posts index page now you can view the data.
In this example I will add the Category and Subcategory select lists to the Add view, so now we need to change this so that the selection in the second list changes according to the selection in the first list.
This will be done via the Js Helper so make it available at the top of the Posts controller (after the Class declaration) with:
public $helpers = array('Js');
You need a Subcategories Controller with a single action to provide the data via AJAX:
request->data['Post']['category_id'];
$subcategories = $this->Subcategory->find('list', array(
'conditions' => array('Subcategory.category_id' => $category_id),
'recursive' => -1
));
$this->set('subcategories',$subcategories);
$this->layout = 'ajax';
}
}
3. Views
The view is a very simple AJAX view that renders the option
tags that go within the select
tag.
$value): ?>
4. Putting it all together
In the Post Add view, file path: View/Posts/add.ctp
we can finally add the Js methods that make the dynamic updating happen. This is the cryptic bit that I struggled with for a few hours as although the CakePHP documentation outlines all the options there are not any complete examples.
Firstly add a categories select element to the form (and change the order of the elements):
Form->input('category_id');
echo $this->Form->input('subcategory_id');
echo $this->Form->input('title');
?>
Add the following code to the bottom of the view, this uses the Js Helper to create the necessary jQuery to perform the updating:
Js->get('#PostCategoryId')->event('change',
$this->Js->request(array(
'controller'=>'subcategories',
'action'=>'getByCategory'
), array(
'update'=>'#PostSubcategoryId',
'async' => true,
'method' => 'post',
'dataExpression'=>true,
'data'=> $this->Js->serializeForm(array(
'isForm' => true,
'inline' => true
))
))
);
?>
This is saying – watch the HTML element with Id PostCategoryId for a change. When it changes update the HTML element with Id PostSubcategoryId with the response from subcategories/GetByCategory
, the data
option is used to send the current value from the initial select element.
Before you leap to test this you need to make changes to the default layout to include jQuery and provide a place for your scripts to be written out.
Html->charset(); ?>
Html->meta('icon');
echo $this->Html->css('cake.generic');
?>
Dynamic Select Box Demonstration
Session->flash(); ?>
element('sql_dump'); ?>
Js, 'writeBuffer')) echo $this->Js->writeBuffer();
// Writes cached scripts
?>
Now you can test the application. On changing the category select list, the subcategory one will automatically update to show the relevant options.