简单afterSave方法不会在Yii中工作 [英] simple afterSave method doesn't work in Yii

查看:1914
本文介绍了简单afterSave方法不会在Yii中工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过 afterSave 方法插入原始更新一个字段,所以我制定以下code。在驱动程序模型

 保护功能afterSave(){
        父母:: afterSave();
        如果($这个 - > isNewRecord){
            $ NEWID =自:: NEWID($这个 - > ID);
            $这个 - > updateByPk($这个 - > ID,新CDbCriteria(阵列('条件'=>'driverid =:driverid','参数'=>阵列('driverid'=> $ NEWID)) ));
        }
}
 

和控制器,污物取得方法:

 公共职能actionCreate()
{
    $模式=新的驱动程序;

    //下面一行的注解,如果需要AJAX验证
    // $这个 - > performAjaxValidation($模式);

    如果(使用isset($ _ POST ['司机']))
    {
        $建模>属性= $ _ POST ['司机'];
        如果($建模>保存())
            $这个 - >重定向(阵列('查看','ID'=> $建模> ID));
    }

    $这个 - >渲染('创造',阵列(
        模式=> $模式,
    ));
}
 

后为什么保存不工作?

编辑:

 私有静态函数CheckNumber($编号){
        $数组=阵列(10);
        为($ i = 0; $ I小于10; $ I ++)
            $数组[$ i] = 0;
        做 {
            $数组[$数%10] ++;
            $数/ = 10;
        }而((int)的($数/ 10)!= 0);
        $数组[$号] ++;

        为($ i = 0; $ I小于10; $ I ++)
            如果($数组[$ I]→1)
                返回false;
        返回true;
    }

公共静态函数NEWID($ ID){
    而(个体经营:: CheckNumber($ ID)== FALSE){
        $ ID ++;
    }
    返回的$ id;
}
 

解决方案

您updateByPk被搞砸了。

检查文档

由于它不知道你要来do..So我要更新通过结果自:: NEWID driver_id价值presuming什么($这个 - > ID); ..

你应该做的是:

 保护功能afterSave(){
    父母:: afterSave();
    如果($这个 - > isNewRecord){
        $ NEWID =自:: NEWID($这个 - > ID);
        $这个 - > driverid = $ new_id里;
        $这个 - > isNewRecord = FALSE;
        $这个 - > saveAttributes(阵列('driverid'));
    }
}
 

和是的,你是对的关于使用 afterSave()

  

isNewRecord是真实的,即使是创造了新的纪录后,但经过第一   的时候,它会是假的始终。

从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..

这篇关于简单afterSave方法不会在Yii中工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆