我想通过方法更新插入的原始字段afterSave
,所以我在驱动程序模型中开发以下代码
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))));
}
}
并在 Controller 中使用 crud 制作的方法:
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;
}