【问题标题】:Unable to code View and Edit actions in Cakephp无法在 Cakephp 中编写查看和编辑操作
【发布时间】:2012-05-15 22:09:36
【问题描述】:

四种型号 餐厅、RestaurantAddresses、RestaurantCuisines 和美食。

我可以通过页面 View/Restaurants/index.ctp 显示所有餐厅、地址和他们的美食类型

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

编辑动作(代码):

编辑有效,但它在 Restaurant_Addresses 和 Cucines 表中添加了两个条目。

公共函数编辑($id = null){ $this->餐厅->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->餐厅->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());


}

【问题讨论】:

    标签: cakephp


    【解决方案1】:

    以下是我将如何编写您的方法。我认为您关于数据未显示的主要问题是 read 方法仅获取第一个模型数据并且没有获取关联数据......所以我会做一个 Model::find(),而不是 Model: :read() 使用包含参数来获取相关数据。

    不要忘记在模型的 $actsAs 参数中添加“可包含”。

    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 并将递归设置为 -1 AppModel。
    • 我仍然无法从其他模型中提取数据:
    • view.ctp 代码行之一是 echo $restaurant['Cusine'] ['name']
    【解决方案2】:

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

    至于编辑,提供链接到帖子数据的样子。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-25
      • 2015-10-27
      • 1970-01-01
      • 2011-09-13
      • 2021-04-10
      相关资源
      最近更新 更多