参考サイトhttp://kwski.net/cakephp-2-x/1088/
[php]
CREATE TABLE IF NOT EXISTS `prefectures` (
`id` INT NOT NULL AUTO_INCREMENT, — id
`prefecture` VARCHAR(8) NOT NULL, — 都道府県
`area` VARCHAR(8) NOT NULL, — 地方
PRIMARY KEY (`id`) )
[/php]
[php]
単一選択のselectボックス
// コントローラ
$this->set( ‘select1’, $this->Prefecture->find( ‘list’, array(
‘fields’ => array( ‘id’, ‘prefecture’)
)));
// ビュー
echo $this->Form->input( ‘prefecture’, array(
‘type’ => ‘select’,
‘options’ => $select1
// ‘selected’ => $selected // 規定値をvalueで指定
));
[/php]
[php]
複数選択できるselectボックス
// コントローラ
$this->set( ‘select1’, $this->Prefecture->find( ‘list’, array(
‘fields’ => array( ‘id’, ‘prefecture’)
)));
// ビュー
echo $this->Form->input( ‘prefecture’, array(
‘type’ => ‘select’,
‘multiple’=> true,
‘options’ => $select1
// ‘selected’ => $selected // 規定値は、valueを配列にしたもの
));
[/php]
[php]
// ビューから受け取ったprefecture配列をカンマ区切りに変換
$this->request->data[‘Model’][‘prefecture’] =
implode(‘,’, $this->data[‘Model’][‘prefecture’]);
// 規定値をビューへ渡す
$this->set(‘selected’, explode(‘,’, $this->data[‘Model’][‘prefecture’]));
[/php]
[php]
selectボックスのオプションをグループ化
// コントローラ
$this->set( ‘select2’, $this->Prefecture->find( ‘list’, array(
‘fields’ => array( ‘id’, ‘prefecture’, ‘area’)
)));
// ビュー
echo $this->Form->input( ‘prefecture’, array(
‘type’ => ‘select’,
‘multiple’=> true, // 複数選択を可能にする場合
‘options’ => $select2
));
[/php]