2011-02-08 131 views
0

我現在有這樣的困惑:與填充選擇現場

酒店控制器

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'))不會產生任何選項。

+0

想通了。 – 2011-02-08 20:08:33

回答

2

這應該是選項列表:

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

嘗試明確設置視圖

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