未在cakephp中调用的更新方法自定义数据源


Update method custom data source not called in cakephp

我正在尝试使用自定义数据源更新数据。但是当我在设置id后调用save时,它正在调用create方法。我还记录了$Model,在这个id是空的。由于id字段为空,将调用create方法而不是update。

下面是我在Model beforeave

中记录$Model时id的值
[useTable] => applications
[id] => 191124
[data] => Array
        (
            [Application] => Array
                (
                    [id] => 191124
                    [Field] => Array
                        (
                            [0] => Array
                                (
                                    [@id] => 13312
                                    [@type] => 1
                                    [@value] => 10317
                                )
                        )
                )
        )

和当我记录$Model在创建id字段是空的

[useTable] => applications
[id] =>
[data] => Array
    (
        [Application] => Array
            (
                [id] => 191124
                [Field] => Array
                    (
                        [0] => Array
                            (
                                [@id] => 13312
                                [@type] => 1
                                [@value] => 10317
                            )
                    )
            )
    )

我正在使用下面的代码呼叫保存

 $data = array(
                    'id' => $archer_id,
                    'Field' => array(
                                            array(
                                                    '@id' => 13312,
                                                    '@type' => 1,
                                                    '@value' => 10317
                                            )
                                    )
                            );
$this->Application->id = $archer_id;
$this->Application->save($data,false);

我使用的模型是

class Application extends ArcherAppModel {
public $hasMany = array('Archer.Assessment');
public $_schema = array(
    'Field' => array('type' => 'array')
);
public function beforeSave( $options = array() ) {
    $this->log(array("message" => "Data in application before save ","data" => $this->data),'debug');
    return true;
} 
}

数据源中的读取函数为

public function read ( Model &$Model, $queryData = array(), $recursive = null ) {
    $this->Model = $Model;
    try {
        $mid = $this->get_module_id();
        $key = $Model->alias . ".id";
        if ( array_key_exists( $key, $queryData['conditions'] ) ) $rid = $queryData['conditions'][$key];
        elseif ( $Model->id ) $rid = $Model->id;
        else $params = $queryData['conditions'];
        $params = $this->_params(array(
            "moduleId" => $mid, 
            "contentId"=>$rid
        ));
        $results = $this->_getSoap('records')->__soapCall('GetRecordById', array('GetRecordById' => $params));
        if ( property_exists( $results, 'GetRecordByIdResult' ) ) return Xml::build($results->GetRecordByIdResult);
        return array();
    } catch ( Exception $e ) {
        $this->log("OH No...");
        $this->log($e->getMessage());
        return array();
    }
}

谢谢。

传递id并不自动意味着它将是一个更新。考虑用mysql保存的情况,其中主键字段是而不是 autoincrement -这种(非常常见的)情况也被CakePHP的模型层支持,其中插入和更新固定的主键值都可以工作。

save方法的工作方式是检查id是否存在,如果不存在,则save方法清除模型id:

if (!$exists && $count > 0) {
    $this->id = false;
}

这是插入操作而不是更新操作的结果。

if (!empty($this->id)) {
    ... update ...
} else {
    ... insert ...
}

问题中提到的自定义数据源没有显示,但它似乎没有实现"这个id存在吗?"。或者模型的exists方法(如果它向数据源发出查询,它只是一个计数)由于不同的原因返回false。

强制更新

要确定它是插入还是更新,请检查为什么Model::exists中的find-by-primary-key调用不起作用并修复数据源,或者覆盖ModelName::exists以实现任何必要的逻辑,即:

// Application Model
public function exists($id = null) {
    if (should be an update) {
        return true;
    }
    return false;
}