想要在yii中保存(false)之后将变量传递给aftersave()方法

时间:2016-07-05 08:30:52

标签: php mysql yii web-deployment

实际上, 如下所示代码, 在模型中,两个函数正在调用

$这 - > sendMailtoTutor() $这 - > sendMailtoLearner()

我想检查模型中的条件,如果该条件为真,那么只有这两个函数都会调用,否则不会。有没有办法传递属性或变量,以便我可以检查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();
}

我该怎么做?

2 个答案:

答案 0 :(得分:1)

您好。查看您是否要afterSave() BookingTemp Model; variable。然后,您可以在model中定义class BookingTemp extends CActiveRecord { public $booking_temp_variable; ............ }

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

现在在actionBooking();

中保存模型之前分配此变量

as

protected function afterSave()
{
    if ($this->isNewRecord)
    {

         $this->booking_temp_variable; // this has your value
    }
    return parent::afterSave();
}

现在在您的后续功能中,您可以轻松地获得它

rules

请勿忘记safe public function rules(){ // your rules array('booking_temp_variable','safe'); }

onItemLongClick()

答案 1 :(得分:0)

你想要做什么?

首先你需要知道,当你调用save()方法时,你会传递两个args - for i in train: im = misc.imread("/.../%s*.jpg" % i) false。第一个arg表示你没有运行验证,第二个是(应该是)属性数组。

  

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