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

时间:2012-05-15 22:09:37

标签: cakephp

四种型号 餐厅,餐厅地址,RestaurantCuisines和美食。

我可以通过页面查看/餐馆/ index.ctp

显示所有餐厅,地址和他们的菜肴类型

但是,我无法编写视图和编辑操作的代码,以下是我用于这两个操作的代码:

编辑操作(代码):

编辑有效,但它在Restaurant_Addresses和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();
    }
}

查看操作(代码):

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

公共功能视图($ 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->Read());


}

2 个答案:

答案 0 :(得分:1)

以下是我编写方法的方法。我认为你的数据没有出现的主要问题是read方法只抓取第一个模型数据并且没有获得相关数据......所以我会做一个Model :: find(),而不是Model: :read()with contains参数也可以获取相关数据。

不要忘记在模型$ 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'))));
}

答案 1 :(得分:0)

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

对于编辑,提供一个指向帖子数据的链接。