YII 1 中的 getPrimaryKey 函数不起作用
getPrimaryKey function in YII 1 is not working
如何在 YII 1 中通过 getPrimaryKey() 获取最后插入的 ID?
我的记录已插入数据库,但返回的是空值。
我使用了下面的代码。
public function saveSurvey($model){
if($model->ID == ''){
$model->Name = trim($model->Name);
$model->EngagementQuestionNo = trim($model->EngagementQuestionNo);
$model->CSVColumnCount = trim($model->CSVColumnCount);
$model->LocationQuestionNo = trim($model->LocationQuestionNo);
$model->CreatedOn = new CDbExpression('NOW()');
if($model->save()){
echo 'saved id >> ';
echo $surveyID = $model->ID;
}
else{
echo 'error';
}
}
}
我当前在模型中的规则
public function rules()
{
return array(
array('Name,EngagementQuestionNo,CSVColumnCount,LocationQuestionNo', 'required'),
array('CSVColumnCount', 'numerical', 'integerOnly'=>true),
array('Name', 'length', 'max'=>100),
array('QuestionNumbers, ID', 'safe'),
array('ID, Name, CreatedOn, LastModifiedOn,QuestionNumbers', 'safe', 'on'=>'search'),
);
}
您只需初始化 Survey 模型的新对象 class
$newObjectModel = new Survey();
添加新对象后,您的代码如下所示:
public function saveSurvey($model){
if($model->ID == ''){
$newObjectModel = new Survey();
$newObjectModel ->Name = trim($model->Name);
$newObjectModel ->EngagementQuestionNo = trim($model->EngagementQuestionNo);
$newObjectModel ->CSVColumnCount = trim($model->CSVColumnCount);
$newObjectModel ->LocationQuestionNo = trim($model->LocationQuestionNo);
$newObjectModel ->CreatedOn = new CDbExpression('NOW()');
if($newObjectModel ->save()){
echo $surveyID = $newObjectModel ->ID;
}
else{
echo 'error';
}
}
}
如何在 YII 1 中通过 getPrimaryKey() 获取最后插入的 ID?
我的记录已插入数据库,但返回的是空值。
我使用了下面的代码。
public function saveSurvey($model){
if($model->ID == ''){
$model->Name = trim($model->Name);
$model->EngagementQuestionNo = trim($model->EngagementQuestionNo);
$model->CSVColumnCount = trim($model->CSVColumnCount);
$model->LocationQuestionNo = trim($model->LocationQuestionNo);
$model->CreatedOn = new CDbExpression('NOW()');
if($model->save()){
echo 'saved id >> ';
echo $surveyID = $model->ID;
}
else{
echo 'error';
}
}
}
我当前在模型中的规则
public function rules()
{
return array(
array('Name,EngagementQuestionNo,CSVColumnCount,LocationQuestionNo', 'required'),
array('CSVColumnCount', 'numerical', 'integerOnly'=>true),
array('Name', 'length', 'max'=>100),
array('QuestionNumbers, ID', 'safe'),
array('ID, Name, CreatedOn, LastModifiedOn,QuestionNumbers', 'safe', 'on'=>'search'),
);
}
您只需初始化 Survey 模型的新对象 class
$newObjectModel = new Survey();
添加新对象后,您的代码如下所示:
public function saveSurvey($model){
if($model->ID == ''){
$newObjectModel = new Survey();
$newObjectModel ->Name = trim($model->Name);
$newObjectModel ->EngagementQuestionNo = trim($model->EngagementQuestionNo);
$newObjectModel ->CSVColumnCount = trim($model->CSVColumnCount);
$newObjectModel ->LocationQuestionNo = trim($model->LocationQuestionNo);
$newObjectModel ->CreatedOn = new CDbExpression('NOW()');
if($newObjectModel ->save()){
echo $surveyID = $newObjectModel ->ID;
}
else{
echo 'error';
}
}
}