这是我的示例代码:
switch($topic_type) {
case 1: //post
$model = 'Userposts';
$field_name = 'user_post_id';
$select_name = 'post_title';
break;
}
$model = ClassRegistry::init($model);
$model->alias = 'TP';
$model->id = $topic_id;
$result = $model->find('first',
array(
'conditions'=>array($field_name=>$topic_id),
'fields'=>array($select_name.' as topic',$field_name.' as id'),
));
if(!empty($result)){
$this->updata[$model_name][$select_name] = $modified_topic;
$this->updata[$model_name][$field_name] = $topic_id;
if($model->save($this->updata)){
$respArr['token'] = $token;
$respArr['status'] = 1;
$respArr['msg'] = 'Success';
echo json_encode(array('token' => $token, 'status' => 1, 'msg' => 'Success'));exit;
}
}
$ model-> find正在工作并获取结果,其中$ model->在find找不到后保存。
field_name是主键,select_name是我想在修改标题后更新的保存。