【问题标题】:simple afterSave method doesn't work in Yii简单的 afterSave 方法在 Yii 中不起作用
【发布时间】:2012-08-29 11:13:38
【问题描述】:

我想通过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))));
        }
}

以及在控制器中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;
}

【问题讨论】:

  • 请显示 self::newid 方法

标签: php mysql activerecord yii


【解决方案1】:

你的 updateByPk 搞砸了..

Check Documentation

由于不清楚你想做什么..所以我假设你想通过刚刚插入行的self::newid($this-&gt;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 即使在创建新记录之后也是 true,但在第一次之后 时间,永远都是假的..

From Yii Forum said By Qiang Himself..

【讨论】:

  • 您的代码出现以下错误:活动记录无法更新,因为它是新记录。
  • 我测试它,得到以下错误:CDbCommand 未能执行 SQL 语句:SQLSTATE[23000]:完整性约束违规:1062 键 'PRIMARY' 的重复条目 '345'。执行的 SQL 语句是:
  • 插入driver (name, fathername, nationalcode, birthdaycity, birthday, education, cellphone, dltype, @98765433@5, workidcompanyworklocationbustypeexperienceyearpractical_class_day_timepractical_class_enter_timepractical_class_exit_timecertificatetimecomputerskillgameskillscore1score2, score3, scoretype, address, phone, imgadd, id, driverid) 值 (:yp0, :yp1, :yp2, :yp3, :yp4, :yp5, :yp6,:yp7,:yp8,:yp9,:yp10,:yp11,:yp12,:yp13,:yp14,:yp15,:yp16,:yp17,:yp18,:yp19,:yp20,:yp21,:yp22 ,
  • 立即尝试..灵感来自yiiframework.com/forum/index.php/topic/…
【解决方案2】:

正如 Rajat 正确指出的那样,您的 updateByPk(); 不正确。您可以改为使用此行来更新新创建记录的 driverid 字段:

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

所以你的整个afterSave() 变成了这样:

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

saveAttributes() 最终调用updateByPk()

【讨论】:

  • +1 此解决方案允许更新而不污染 isNewRecord 属性
【解决方案3】:

如果插入成功,“CActiveRecord”中的insert方法将“isNewRecord”属性设置为“false”。

因此,您的 afterSave 方法中的“if ($this->isNewRecord)”条件始终为 false,并且程序永远不会在此条件内执行 updateByPk 方法。这就是为什么您的 afterSave 不起作用的原因。

【讨论】:

    【解决方案4】:
    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();
            }
    

    【讨论】:

    • 虽然这段代码 sn-p 可以解决问题,但including an explanation 确实有助于提高帖子的质量。请记住,您正在为将来的读者回答问题,而这些人可能不知道您的代码建议的原因。也请尽量不要用解释性的 cmets 挤满你的代码,因为这会降低代码和解释的可读性!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-20
    • 2020-04-11
    • 1970-01-01
    • 2013-11-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多