关联模型中的CakePHP下拉框

关联模型中的CakePHP下拉框,cakephp,Cakephp,我有一个简单的api,允许客户预订车辆。在我的预订控制器中,我有一个添加预订的功能。代码如下: 控制器 function add() { if($this->request->is('post')) { if($this->Reservation->save($this->data)) { $this->Session->setFlash('The Reservation w

我有一个简单的api,允许客户预订车辆。在我的预订控制器中,我有一个添加预订的功能。代码如下:

控制器

    function add() {
        if($this->request->is('post')) {
            if($this->Reservation->save($this->data)) {
                $this->Session->setFlash('The Reservation was successfully added!');
                $this->redirect(array('action'=>'index'));
            } else {
                $this->Session->setFlash('The Reservation was not added.');
            }
        }
        $this->set('title_for_layout','Add Reservation');
    }
以下是我在“添加”视图中的预订内容

查看

<?php
echo $this->Form->create('Reservation', array('action'=>'add'));
echo $this->Form->input('customer_id', array( 'type' => 'text' ) );
echo $this->Form->input('vehicle_id', array( 'type' => 'text' ) );
echo $this->Form->input('date');
echo $this->Form->end('Add Reservation');
?>

有人能给我指出正确的方向吗?这样,在添加预订页面上将出现一个下拉框,其中包含车辆和客户ID的列表。

在控制器的添加功能中,使用

$vehicle = $this->Reservation->Vehicle->find('list', array('fields' =>array('Vehicle.id','Vehicle.name')));                               
$this->set('vehicle', $vehicle);
在您看来,使用

<?php echo $this->Form->input('vehicle_id', array('type' => 'select',
                                                  'options' => $vehicle)); ?>


对客户也使用同样的方法,您将在下拉列表中获得客户和车辆列表。

命名变量
车辆将更容易。
。按照惯例,
vehicle\u id
下拉列表将自动填充这些值,而无需指定
选项
选项。
<?php echo $this->Form->input('vehicle_id', array('type' => 'select',
                                                  'options' => $vehicle)); ?>