无法在Cakephp中编写查看和编辑操作的代码

无法在Cakephp中编写查看和编辑操作的代码,cakephp,Cakephp,四种模式 餐厅,餐厅服装,餐厅指南和菜肴 我可以通过页面View/restaurants/index.ctp显示所有餐厅、餐厅地址及其菜系类型 但是,我无法对查看和编辑操作进行编码,以下是我对这两个操作使用的代码: 编辑操作(代码): 编辑工作,但它增加了两个多条目在餐厅_地址和Cucines表 公共函数编辑($id=null){ $this->Restaurant->id=$id $this->loadModel('Cusine');

四种模式 餐厅,餐厅服装,餐厅指南和菜肴

我可以通过页面View/restaurants/index.ctp显示所有餐厅、餐厅地址及其菜系类型

但是,我无法对查看和编辑操作进行编码,以下是我对这两个操作使用的代码:

编辑操作(代码):

编辑工作,但它增加了两个多条目在餐厅_地址和Cucines表

公共函数编辑($id=null){ $this->Restaurant->id=$id

              $this->loadModel('Cusine');
            $model_cusine_edit_data = $this->Cusine->find('list');
            $this->set('CusineEditList', $model_cusine_edit_data);



    if (!$this->Restaurant->exists()) {
        throw new NotFoundException('Invalid Restaurant');
    }

    if ($this->request->is('post') || $this->request->is('put')) {              

        $this->Restaurant->save($this->request->data);

            $this->Session->setFlash('The restaurant has been saved');
            $this->redirect(array('action' => 'index'));
        } else {
    $this->request->data = $this->Restaurant->read();
    }
}
    if (!$this->Restaurant->exists()) {
        throw new NotFoundException('Invalid user');
    }

    if (!$id) {
        $this->Session->setFlash('Invalid user');
        $this->redirect(array('action' => 'index'));
    }
    $this->set('restaurant', $this->Restaurant->Read());


}
查看操作(代码):

以下代码仅显示餐厅名称信息,无法查看街道和邮政编码

公共函数视图($id=null){ $this->Restaurant->id=$id

              $this->loadModel('Cusine');
            $model_cusine_edit_data = $this->Cusine->find('list');
            $this->set('CusineEditList', $model_cusine_edit_data);



    if (!$this->Restaurant->exists()) {
        throw new NotFoundException('Invalid Restaurant');
    }

    if ($this->request->is('post') || $this->request->is('put')) {              

        $this->Restaurant->save($this->request->data);

            $this->Session->setFlash('The restaurant has been saved');
            $this->redirect(array('action' => 'index'));
        } else {
    $this->request->data = $this->Restaurant->read();
    }
}
    if (!$this->Restaurant->exists()) {
        throw new NotFoundException('Invalid user');
    }

    if (!$id) {
        $this->Session->setFlash('Invalid user');
        $this->redirect(array('action' => 'index'));
    }
    $this->set('restaurant', $this->Restaurant->Read());


}

对于视图,您需要执行
find('first',array('conditions'=>array('Restaurant.id'=>$id),'recursive'=>2));
或者,更好的方法是使用
ContainableBehavior
,它允许您选择要获取的模型。在书中搜索用法和示例


至于编辑,请提供一个指向post数据的链接。

以下是我将如何编写您的方法。我认为,关于数据未显示,您的主要问题是read方法只获取第一个模型数据,而不获取相关数据……因此,我将使用Model::find(),而不是Model::read()使用contain参数也可以获取关联的数据

别忘了在模型$actsAs参数中添加“Containable”

public function edit($id=null){
    $this->Restaurant->id = $id;
    if (!$this->Restaurant->exists()) {
        throw new NotFoundException('Invalid Restaurant');
    }

     if(!empty($this->request->data)){
          if($this->Restaurant->save($this->request->data)){
              $this->Session->setFlash('The restaurant has been saved');
              $this->redirect(array('action' => 'index'));
          } else {
              $this->Session->setFlash('Unable to save the restaurant');
          }
     }


     $model_cusine_edit_data = $this->Restaurant->RestaurantCusine->Cusine->find('list');
     $this->set('CusineEditList', $model_cusine_edit_data);

     $this->request->data = $this->Restaurant->find('first', array('conditions'=>array('Restaurant.id'=>$id), 'contain'=>array('RestaurantAddresses','RestaurantCusine')));
}

public function view($id=null){
    $this->Restaurant->id = $id;

    if (!$this->Restaurant->exists()) {
        throw new NotFoundException('Invalid user');
    }

    if (!$id) {
        $this->Session->setFlash('Invalid user');
        $this->redirect(array('action' => 'index'));
    }

    $this->set('restaurant', $this->Restaurant->find('first', array('conditions'=>array('Restaurant.id'=>$id), 'contain'=>array('RestaurantAddresses','RestaurantCusine'))));
}

可能更需要添加Containable并将recursive设置为-1 AppModel。我仍然无法从其他模型中提取数据:view.ctp代码行之一是echo$restaurant['Cusine']['name']