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'])
更多推荐
插入新行而不是更新当前行
发布评论