插入新行而不是更新当前行

编程入门 行业动态 更新时间:2024-10-19 17:27:52
本文介绍了插入新行而不是更新当前行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

hi我在下面的代码有问题我正在做一个Ajax,如果数据完全相同,它不插入一个记录。但是如果文本区域中的文本更改,则应更新记录,而是插入新行。

这里是我的模型

<?php class Test extends AppModel { var $ belongsTo = array('table1','table2' ); var $ validate = array('field1'=> array('rule'=>'duplicateCheck','message'=& 'duplicate record'),'textbody'=> array('rule'=>'duplicateCheck','message'=& )); function duplicateCheck(){ $ conditions = array('Test.field1'=> $ this-> data ['Test'] ['field1'] ,'Test.textbody'=> $ this-> data ['Test'] ['textbody'] ); return!$ this-> hasAny($ conditions); } } ?>

这里是我的控制器

<?php function add(){ if(isset($ this-> data)){ if($ this-> Test-> save($ this-> data ['Test'])){ } else { if(isset($ this-> Test-> validationErrors ['field1'])){ } } } } ?>

不知道如何处理这个感谢

EDIT

确定我将其更改为此字段,但现在不更新字段

var $ belongsTo = array('table1','table2'); var $ validate = array('field1'=> array('rule'=>'duplicateCheck','message'=& 'duplicate record'),'userid'=> array('rule'=>'duplicateCheck','message'=& )); function duplicateCheck(){ $ conditions = array('Test.field1'=> $ this-> data ['Test'] ['field1'] ,'Test.userid'=> $ this-> data ['Test'] ['userid'] ); return!$ this-> hasAny($ conditions); }

} ?>

Model-> save(),否则它将为PK字段创建一个新的ID。此外,$ this-> data应该被发送到保存为$ this-> data包含模型的名称。因此,如果您的模型名称为Test,请尝试发送:

$ this-> Test-> save - > data)

NOT

$ this-> Test-> save($ this-> data ['Test'])

hi I am having problem with the code below I am doing ajax and if the data is exactly same it doesnt insert a record. But if the text in text area changes, it should update the record, but instead it inserts a new row.

here is my model

<?php class Test extends AppModel { var $belongsTo = array('table1','table2'); var $validate = array( 'field1' => array( 'rule' => 'duplicateCheck', 'message' => 'Duplicate record' ), 'textbody' => array( 'rule' => 'duplicateCheck', 'message' => 'Duplicate record' ) ); function duplicateCheck() { $conditions = array( 'Test.field1' => $this->data['Test']['field1'], 'Test.textbody' => $this->data['Test']['textbody'] ); return !$this->hasAny($conditions); } } ?>

and here is my controller

<?php function add() { if(isset($this->data)) { if($this->Test->save($this->data['Test'])) { } else { if(isset($this->Test->validationErrors['field1'])) { } } } } ?>

not sure how I can tackle this thanks

EDIT

ok I change it to this but now it wont update the field

var $belongsTo = array('table1','table2'); var $validate = array( 'field1' => array( 'rule' => 'duplicateCheck', 'message' => 'Duplicate record' ), 'userid' => array( 'rule' => 'duplicateCheck', 'message' => 'Duplicate record' ) ); function duplicateCheck() { $conditions = array( 'Test.field1' => $this->data['Test']['field1'], 'Test.userid' => $this->data['Test']['userid'] ); return !$this->hasAny($conditions); }

} ?>

解决方案

Make sure you are passing the PK to the $this->Model->save() or it will create a new ID for the PK field. Also, the $this->data should be sent to the save as $this->data with the name of the model included. So if your model name is Test, try sending it like:

$this->Test->save($this->data)

NOT

$this->Test->save($this->data['Test'])

更多推荐

插入新行而不是更新当前行

本文发布于:2023-10-26 23:42:58,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1531687.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:而不是

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!