希望在yii中的save(false)之后将变量传递给aftersave()方法


want to pass variable to aftersave() method after save(false) in yii

实际上,正如您在下面的代码中看到的,在模型中,两个函数调用

$this->sendMailtoTutor(($this->sendMailtoLearner((TR

我想检查模型中的一个条件,如果该条件为真,那么只有这两个函数才会调用,否则就不会调用。有没有什么方法可以传递属性或变量,这样我就可以在aftersave((中检查条件了。我的代码如下

在控制器中

public function actionBooking()
{
    $booking_temp = new BookingTemp();
    $this->performAjaxValidation($booking_temp);
    if (Yii::app()->request->isPostRequest && Yii::app()->request->getPost('BookingTemp'))
    {
        $booking_temp->attributes = Yii::app()->request->getPost('BookingTemp');

        //echo '<pre>';print_r($booking_temp->attributes);exit;
        if ($booking_temp->validate())
        {
            $extra_price = 0;
            $post_data = Yii::app()->request->getPost('BookingTemp');
            $cam = Cam::model()->findByPk($post_data['temp_cam_id']);
            $data = array();
            $data = $post_data;
            $data['temp_book_user_id'] = Yii::app()->user->id;
            $data['temp_book_cam_price'] = $cam->cam_price;
            $data['temp_book_duration'] = $cam->cam_duration;
            if ($post_data['temp_book_session'] == 2) {
                $data['temp_book_cam_price'] = 2 * $cam->cam_price;
                $data['temp_book_duration'] = 2 * $cam->cam_duration;
            }
            if ($post_data['temp_book_is_extra'] == "Y") {
                $extra_price = $cam->camExtras->extra_price;
                $data['temp_book_extra_price'] = $extra_price;
            }
            $price_calculation = CamBooking::price_calculation(Yii::app()->user->country_id, $data['temp_book_cam_price'], $extra_price);
            $data['temp_book_processing_fees'] = $price_calculation['processing_fees'];
            $data['temp_book_service_tax'] = $price_calculation['service_tax'];
            $data['temp_book_total_price'] = $price_calculation['total_price'];
            $booking_temp->temp_value = serialize($data);
            $booking_temp->user_id = Yii::app()->user->id;
            $booking_temp->tutor_id = $cam->tutor_id;
            $booking_temp_variable = 'check_aftersave';
            $booking_temp->save(false, $booking_temp_variable);
            //$booking_temp->saveAttributes(array('tutor_id', 'user_id', 'temp_value'));
            $created_at = Yii::app()->localtime->fromUTC($booking_temp->created_at);
            $created_at_time = strtotime($created_at);
            $end_time = $created_at_time + (60 * 3); // 3 min greater from created
            $end_time_format = date("Y/m/d H:i:s", $end_time);
            echo json_encode(array(
                'status' => 'success',
                'temp_guid' => $booking_temp->temp_guid,
                'end_time_format' => $end_time_format,
                    ), JSON_UNESCAPED_SLASHES);
            Yii::app()->end();
        } else {
            $error = CActiveForm::validate($booking_temp);
            if ($error != '[]')
                echo $error;
            Yii::app()->end();
        }
    }
}

在模型

protected function afterSave()
{
    if ($this->isNewRecord)
    {
        $this->sendMailtoTutor();
        $this->sendMailtoLearner();
        if ($this->is_message == 'Y' && !empty($this->book_message))
        {
            Message::insertMessage($this->book_message, $this->book_user_id, $this->cam->tutor_id, $this->cam_id);
        }
        $user_profile_link = CHtml::link($this->bookUser->fullname, array("/site/user/profile", "slug" => $this->bookUser->slug));
        $cam_link = CHtml::link($this->cam->cam_title, array("/site/cam/view", "slug" => $this->cam->slug));
        $message = "You have a new booking from {$user_profile_link} for your {$cam_link}";
        Notification::insertNotification($this->cam->tutor_id, $message, 'book', $this->book_id);
    }
    return parent::afterSave();
}

我该如何执行此操作?

嗨,看看你是否想在BookingTempModel;的afterSave()中进行;。然后,您可以在类似model的中定义variable

class BookingTemp extends CActiveRecord
{
public $booking_temp_variable;
............
}

现在,在actionBooking((中保存模型之前,先分配这个变量;

作为

$booking_temp->booking_temp_variable = 'check_aftersave';
$booking_temp->save(false);// no need to pass a varible

现在在你的后存功能中,你可以像一样轻松地获得它

protected function afterSave()
{
    if ($this->isNewRecord)
    {
         $this->booking_temp_variable; // this has your value
    }
    return parent::afterSave();
}

别忘了把它作为safe 保存在rules

public function rules(){
// your rules
array('booking_temp_variable','safe');
}

你到底想做什么?

首先,您必须知道,当您调用save((方法时,u会向其中传递两个参数——false$booking_temp_variable。第一个参数表示不运行验证,第二个参数是(应该是(属性数组。

http://www.yiiframework.com/doc/api/1.1/CActiveRecord#save-详细