Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/kubernetes/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Forms 如何在每次更新记录后仅在表单中执行一次添加操作_Forms_Cakephp_Element - Fatal编程技术网

Forms 如何在每次更新记录后仅在表单中执行一次添加操作

Forms 如何在每次更新记录后仅在表单中执行一次添加操作,forms,cakephp,element,Forms,Cakephp,Element,我有一个表单元素。当我单击submit按钮时,它会将数据添加到表中。 这很好。但我需要的是在每次提交数据后,仅在第一次向表中添加数据。我们如何才能做到这一点 function add(){ if(!empty($this->data)){ $this->loadModel('Defineroute'); $this->Defineroute->create(); $this->Defineroute->save($route_dat

我有一个表单元素。当我单击submit按钮时,它会将数据添加到表中。 这很好。但我需要的是在每次提交数据后,仅在第一次向表中添加数据。我们如何才能做到这一点

function add(){
  if(!empty($this->data)){
    $this->loadModel('Defineroute');
    $this->Defineroute->create();
    $this->Defineroute->save($route_data); //route_data is data from the form
  }
}
我还有一个疑问,控制器中有一个view.ctp,我有一个view()函数。但是在view.ctp中,我使用了一个表单元素


我们如何在控制器中为该元素编写函数?当我尝试编写函数时,它会显示缺少视图的错误。

CakePhp根据您保存的数据中是否存在主键(“id”)来确定是更新现有记录还是插入新记录

这将插入一条新记录:

$route_data = array(
    'Defineroute' => array(
        'name' => 'I am a new record'
    )
);

$this->Defineroute->save($route_data);
这将更新现有记录

$route_data = array(
    'Defineroute' => array(
        'id'   => 123,
        'name' => 'I will update record with ID 123'
    )
);

$this->Defineroute->save($route_data);
为了在单个表单中适应此情况,请在视图/元素中创建表单,并且仅在编辑现有记录时为
id
字段添加输入


另请参见

@SibinF,这取决于您的情况;如果要编辑,您将在何处检索现有数据?如何决定是否要编辑或插入记录?如何“选择”要编辑的正确记录?