如何在一个视图中显示两个模型值并通过一个控制器更新两个模型值

how does visible two models value in one view and update two models value by one controller

我有两个模型 Group_onesGroup_twos。我在 ac_config.ctp 文件中显示此值。

我的控制器代码如下

public function ac_config($id = null)
    {
        if (!$id) {
            $this->Session->setFlash('Please provide a Site id');
            $this->redirect(array('action'=>'dashboard'));
        }

        $site_id_1          = $this->GroupOne->findById($id);
        $site_name          = $site_id_1['GroupOne']['site_name'];
        $ac_one_time        = $site_id_1['GroupOne']['ac_on_time_one'];
        $group_one_active   = $site_id_1['GroupOne']['active'];

        $site_id_2          = $this->GroupTwo->findById($id);
        $ac_two_time        = $site_id_2['GroupTwo']['ac_on_time_two']; 
        $group_two_active   = $site_id_2['GroupTwo']['active'];


        if (!$site_id_1) {
            $this->Session->setFlash('Invalid Site ID Provided');
            $this->redirect(array('action'=>'dashboard'));
        }

        if (!$site_id_2) {
            $this->Session->setFlash('Invalid Site ID Provided');
            $this->redirect(array('action'=>'dashboard'));
        }

        if ($this->request->is('post') || $this->request->is('put')) {
            $this->GroupOne->id = $id;
            $this->GroupTwo->id = $id;

            if (($this->GroupTwo->save($this->request->data)) || ($this->GroupOne->save($this->request->data))) {
                $this->Session->setFlash(__('AC Configuration has been update'));
                $this->redirect(array('action' => 'ac_config', $id));
            }
            else
            {
                $this->Session->setFlash(__('Unable to AC Configuration has been update.'));
            }
        }

        $this->set(compact('site_name','ac_one_time','group_one_active'));
        $this->set(compact('ac_two_time','group_two_active'));

        $this->set('group_one', $site_id_1);
        $this->set('group_two', $site_id_2);

        if (!$this->request->data) {
            $this->request->data = $site_id_1;
            $this->request->data = $site_id_2;  
        }
    }

我的观看内容如下

当我按下 save 按钮时出现问题。它只在数据库中保存 group-2 表单值。当 AC Run TimeAC Rest Time 显示 Group-2 时,表单值保存在 Model Group_twosAC Run TimeAC Rest Time 的数据库中,然后显示 Group-1表单值保存在 Model Group_ones 的数据库中。

下面还有一个问题

if (!$this->request->data) {
    $this->request->data = $site_id_1;
    $this->request->data = $site_id_2;  // this show rest time, run time
}

如果我写上面的代码然后我得到

if (!$this->request->data) {
    $this->request->data = $site_id_2;
    $this->request->data = $site_id_1;  //this show rest time, run time.
} 

同样,如果我在上面编写这段代码,那么我会得到

但我需要 AC Run Time and AC Rest Time 两个组同时,当我单击保存按钮时,对应组值的任何两个组都保存在模型中。我的逻辑有什么问题。

感谢您的帮助。

在设置请求数据之前简单地合并两个数组:

if (!$this->request->data) { $this->request->data = array_merge($site_id_1, $site_id_2); }

如果这没有帮助,那么查看您的视图代码也会很有用。

根据您的代码,您正在覆盖请求数据数组。您需要做的是,您必须分别设置两个数组或合并到单个数组中。

在您当前的代码下面用 $site_id_2 覆盖 $site_id_1:

 $this->request->data = $site_id_1;
 $this->request->data = $site_id_2;

现在您可以通过以下方式传递数据:

$this->request->data['site_id_1'] = $site_id_1;
$this->request->data['site_id_2'] = $site_id_2;

$this->request->data = array_merge($site_id_1, $site_id_2);

你只需要改变这两行。

此外,我不知道您如何在视图中获取两种模型的结果,但您可以像下面这样分别获取数据:

$site_id_1 = $this->request->getData('site_id_1');
$site_id_2 = $this->request->getData('site_id_2');