【问题标题】:inserting a new row instead of updating the current row插入新行而不是更新当前行
【发布时间】:2011-04-26 00:31:17
【问题描述】:

您好,下面的代码有问题 我正在做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' => 'Duplicate record'
            )
        );

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



            }
        }
    }



}   

?>

不知道如何解决这个问题 谢谢

编辑

好的,我将其更改为这个,但现在它不会更新该字段

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

} ?>

【问题讨论】:

  • 不应该保存($this->data)吗?否则最好先尝试不复杂的ajax,更容易检查sql日志等......

标签: cakephp cakephp-1.3


【解决方案1】:

确保您将 PK 传递给 $this->Model->save() ,否则它将为 PK 字段创建一个新 ID。此外,$this->data 应该以 $this->data 的形式发送到保存,其中包含模型的名称。因此,如果您的型号名称是 Test,请尝试发送如下:

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

不是

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

【讨论】:

    【解决方案2】:

    如果 $this->data['Test']['id'] 有值,save 函数将更新记录而不是创建新记录。

    由于只有当 Test.field1 已经在表中时才会更新记录,所以需要在 add() 函数中进行另一次检查:

    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】:

      在您的模型中尝试以下代码:

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

      假设您有一个名为“field_name”的字段。然后在您的模型中,您只需放置一个验证规则,它就会自动处理。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-07-03
        相关资源
        最近更新 更多