未在cakephp中调用Update方法自定义数据源
我正在尝试使用自定义数据源更新数据。但是,当我在设置id后调用save时,它正在调用create方法。我还记录了$Model,其中id为空。由于id字段为null,因此将调用create方法而不是update 下面是我在保存之前在模型中登录$Model时id的值未在cakephp中调用Update方法自定义数据源,php,cakephp,model-view-controller,cakephp-2.0,datasource,Php,Cakephp,Model View Controller,Cakephp 2.0,Datasource,我正在尝试使用自定义数据源更新数据。但是,当我在设置id后调用save时,它正在调用create方法。我还记录了$Model,其中id为空。由于id字段为null,因此将调用create方法而不是update 下面是我在保存之前在模型中登录$Model时id的值 [useTable] => applications [id] => 191124 [data] => Array ( [Application] => Array
[useTable] => applications
[id] => 191124
[data] => Array
(
[Application] => Array
(
[id] => 191124
[Field] => Array
(
[0] => Array
(
[@id] => 13312
[@type] => 1
[@value] => 10317
)
)
)
)
当我在createid字段中记录$Model时,它是空的
[useTable] => applications
[id] =>
[data] => Array
(
[Application] => Array
(
[id] => 191124
[Field] => Array
(
[0] => Array
(
[@id] => 13312
[@type] => 1
[@value] => 10317
)
)
)
)
我正在使用下面的代码调用save
$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;
}
}
数据源代码中的Read函数为
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保存的情况,其中主键字段不是自动增量的。这个(非常常见的)情况也由CAKEPHP的模型层支持,其中插入和更新具有固定的主键值都有效。 “保存”方法的工作方式是,如果不使用“保存”方法,则执行以下操作: 这样做的后果 问题中提到的自定义数据源未显示,但它似乎未实现“此id是否存在?”。该方法或模型的exists方法()由于不同的原因返回false 强制更新 要确定它是否是更新的插入,请检查
Model::exists
中的按主键查找调用为何不起作用并修复数据源,或者重写ModelName::exists
以实现任何必要的逻辑,即:
// Application Model
public function exists($id = null) {
if (should be an update) {
return true;
}
return false;
}
if (!empty($this->id)) {
... update ...
} else {
... insert ...
}
// Application Model
public function exists($id = null) {
if (should be an update) {
return true;
}
return false;
}