inserting a new row instead of updating the current row

点点圈 提交于 2019-12-11 00:43:03

问题


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);
    }

} ?>


回答1:


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'])



回答2:


If $this->data['Test']['id'] has a value, the save function will update the record instead of creating a new record.

Since you will only update record if Test.field1 is already in the table, you need to do another checking inside the add() function:

function add() {
    if(isset($this->data)) {
        $existingRecordId = $this->Test->find('first', array(
            'conditions' => array('Test.field1' => $this->data['Test']['field1']),
            'fields' => array('Test.id')
        ));
        if(sizeof($existingRecordId)>0)
            $this->data['Test']['id'] = $existingRecordId['Test']['id'];
        if($this->Test->save($this->data['Test'])) {
        } else {
            if(isset($this->Test->validationErrors['field1'])) {

            }
        }
    }
}



回答3:


Try this code in your model:

'field_name' => array(
        'alphanumeric' => array(

            'rule' => array('isUnique'),

            'message' => 'Percentage Commision already set for this api',

            'allowEmpty' => false,

            //'required' => false,

            //'last' => false, // Stop validation after this rule

            //'on' => 'create', // Limit validation to 'create' or 'update' operations

        ),

Suppose you have a field called "field_name". then in your model you can just put a validation rule and it will take care automatically.



来源:https://stackoverflow.com/questions/5784718/inserting-a-new-row-instead-of-updating-the-current-row

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!