简体   繁体   中英

simple afterSave method doesn't work in Yii

I wanna update a field in inserted raw by afterSave method,so I develop following code in 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))));
        }
}

and in Controller the method which crud made:

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,
    ));
}

why after save doesn't work?

edit:

    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;
}

Your updateByPk is messed up..

Check Documentation

As its not clear what you want to do..So I am presuming you want to update driver_id value by result of self::newid($this->id); of just inserted row..

what you should do is:

protected function afterSave() {
    parent::afterSave();
    if ($this->isNewRecord) {
        $newid = self::newid($this->id);
        $this->driverid = $new_id;
        $this->isNewRecord = false;
        $this->saveAttributes(array('driverid'));
    }
}

And yeah you are right about usage of 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..

As rightly pointed out by Rajat, your updateByPk(); is incorrect. You can instead use this line, to update the driverid field of the newly created record:

$this->updateByPk($this->id,array('driverid'=>$newid));

So your entire afterSave() becomes this:

protected function afterSave() {
    parent::afterSave();
    if ($this->isNewRecord) {
        $newid = self::newid($this->id);
        $this->updateByPk($this->id,array('driverid'=>$newid));
    }
}

saveAttributes() ultimately calls updateByPk() .

The insert method in "CActiveRecord" sets the "isNewRecord" property as "false" if the insertion is done successfully.

Therefore the "if ($this->isNewRecord)" condition in your afterSave method is always false and the program will never execute the updateByPk method inside this condition. That's why your afterSave won't work.

public function afterSave(){
        if($this->isNewRecord) {
            $transaction_entry = new TransactionEntry;
            $transaction_entry->branch_id = 1;
            $transaction_entry->date_of_transaction = $this->bill_date;
            $transaction_entry->account_category_id = 1;
            $transaction_entry->description = $this->student->admission_no ."-".$this->course->course_name."-".$this->fee_month."/".$this->fee_year;
            $transaction_entry->amount = $this->total_amount;
            $transaction_entry->save();
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM