我想通过afterSave方法更新插入raw中的字段,因此我在Driver Model中开发了以下代码
protected function afterSave() {
parent::afterSave();
if ($this->isNewRecord) {
$newid = self::newid($this->id);
$this->updateByPk($this->id, new CDbCriteria(array('condition'=>'driverid = :driverid', 'params'=>array('driverid'=> $newid))));
}
}
以及在控制器中使用的方法:
public function actionCreate()
{
$model=new Driver;
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if(isset($_POST['Driver']))
{
$model->attributes=$_POST['Driver'];
if($model->save())
$this->redirect(array('view','id'=>$model->id));
}
$this->render('create',array(
'model'=>$model,
));
}
为什么保存后不起作用?
编辑:
private static function CheckNumber($number) {
$array = array(10);
for ($i = 0; $i < 10; $i++)
$array[$i] = 0;
do {
$array[$number % 10]++;
$number /= 10;
} while ((int) ($number / 10 ) != 0);
$array[$number]++;
for ($i = 0; $i < 10; $i++)
if ($array[$i] > 1)
return false;
return true;
}
public static function newid($id) {
while (self::CheckNumber($id) == FALSE) {
$id++;
}
return $id;
}
解决方法:
你的updateByPk搞砸了……
因为它不清楚你想要做什么..所以我假设你想通过self :: newid($this-> id)的结果更新driver_id值;刚插入的行..
你应该做的是:
protected function afterSave() {
parent::afterSave();
if ($this->isNewRecord) {
$newid = self::newid($this->id);
$this->driverid = $new_id;
$this->isNewRecord = false;
$this->saveAttributes(array('driverid'));
}
}
是的,你对afterSave的使用是正确的()
isNewRecord is true even after new record is created, but after first
time, it will be false always..
From Yii Forum said By Qiang Himself..
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。