与填充选择字段混淆

时间:2011-02-08 19:08:16

标签: cakephp

我目前有这个:

酒店控制器

class HotelsController extends AppController {

    var $name = 'Hotels';

    function admin_add() {
        $this->set('hotel_categories', $this->Hotel->HotelCategory->find('list'));

        if ( ! empty($this->data)) {

            $this->data['Page']['title'] = $this->data['Hotel']['title'];
            $this->data['Page']['layout'] = 'index';

            if ($this->Hotel->saveAll($this->data)) {
                $this->Session->setFlash('Your hotel has been saved', 'flash_good');
                $this->redirect(array('action' => 'admin_add'));
            }
        }
    }

HotelCategory模型

class HotelCategory extends AppModel {
    var $name = 'HotelCategory';

    var $hasAndBelongsToMany = array(
        'Hotel' => array(
            'className' => 'Hotel'
        )
    );

酒店模特

class Hotel extends AppModel {
    var $name = 'Hotel';
    var $hasAndBelongsToMany = array(
        'HotelCategory' => array(
            'className' => 'HotelCategory'
        )
    );

查看

<div id="main">
        <h2>Add Hotel</h2>
        <?php echo $this->Session->flash();?>
        <div>
        <?php
        debug($hotel_categories);
        echo $this->Form->create('Hotel');
        echo $this->Form->input('Hotel.title');
        echo $this->Form->input('HotelCategory', array('options' => 'select', 'multiple' => 'checkbox'));
        echo $this->Form->input('Hotel.body', array('rows' => '3'));

        echo $this->Form->input('Page.meta_keywords');
        echo $this->Form->input('Page.meta_description');

        echo $this->Form->end('Save Hotel');
        ?>
        </div>
<!-- main ends -->
</div>

debug($hotel_categories);有值时,我可以确认。

我遇到的问题是$this->Form->input('HotelCategory', array('options' => 'select', 'multiple' => 'checkbox'))没有产生任何选项。

2 个答案:

答案 0 :(得分:2)

应该是:

echo $this->Form->input('HotelCategory', array(
                                            'type' => 'select', 
                                            'multiple' => 'checkbox',
                                            'options'=>$hotel_categories));

答案 1 :(得分:1)

尝试在视图中明确设置选项列表

<?php echo $this->Form->input('HotelCategory', array(
'type'=>'select', 
'options' => $hotel_categories, 
'multiple' => true)); ?>