Yii2:如何在model-> save()之后执行sql查询

public function actionCreate()
{
$ model = new CreateBookings();
\ n if($ model-> load(Yii :: $ app-> request-> post())){
$ imageName = $ model-> first_name;
$ mobile = $ model-> primary_mobile;
$ type = $ model-> room_type;

$ model-> file = UploadedFile :: getInstance($ model,'file');
$ model-> file-> saveAs ('uploads / id_images /'。$ imageName。'_'。$ mobile。'。'。$ model-> file-> extension);
//保存db列中的路径
$ model- > id_image ='uploads / id_images /'。 $ imageName。 '_'。 $ mobile。 '' 。 $ model-> file-> extension;
$ model-> save();

返回$ this-> redirect(['view','id'=> $ model-> id]);
} else {
return $ this-> render('create',[
'model'=> $ model,
]);
}
\ n}
</ code> </ pre>

我需要执行查询 \ Yii :: $ app-&gt; db-&gt; createCommand(“UPDATE room_types SET total_booked = total_booked + 1 WHERE room_type ='$ model-&gt; room_type'“) - &gt;执行(); </ code>在model-&gt; save(); </ p>

相同的查询工作 actionupdate()但不在actioncreate()中,
是否可以使用行为?</ p>
</ div>

展开原文

原文

    public function actionCreate()
        {
            $model = new CreateBookings();

            if ($model->load(Yii::$app->request->post())) {
                $imageName = $model->first_name;
                $mobile    = $model->primary_mobile;
                $type      = $model->room_type;

                $model->file = UploadedFile::getInstance($model, 'file');
                $model->file->saveAs('uploads/id_images/' . $imageName . '_' . $mobile . '.' . $model->file->extension);
                //save the path in the db column
                $model->id_image = 'uploads/id_images/' . $imageName . '_' . $mobile . '.' . $model->file->extension;
                $model->save();



                return $this->redirect(['view', 'id' => $model->id]);
            } else {
                return $this->render('create', [
                    'model' => $model,
                ]);
            }

        }

I need to execute query \Yii::$app->db->createCommand("UPDATE room_types SET total_booked = total_booked + 1 WHERE room_type = '$model->room_type' ")->execute(); after the model->save();

same query working in actionupdate() but not in actioncreate(), Is it possible with behaviours?

1个回答



您可以覆盖ActiveRecord的 afterSave </ code>方法或直接在db上创建触发器</ p>
\ n

文档: http:/ /www.yiiframework.com/doc-2.0/yii-db-baseactiverecord.html#afterSave%28%29-detail </ p>

  public function afterSave($ insert)  ,$ changedAttributes)
{
parent :: afterSave($ insert,$ changedAttributes);

//您的代码在这里
Yii :: $ app-&gt; db-&gt; createCommand(sprintf(“UPDATE” room_types SET total_booked = total_booked + 1 WHERE room_type ='%s'“,$ this-&gt; room_type)) - &gt; execute();

}
</ code> </ pre>
</ DIV>

展开原文

原文

You could override afterSave method of ActiveRecord or create a trigger directly on db

Documentation: http://www.yiiframework.com/doc-2.0/yii-db-baseactiverecord.html#afterSave%28%29-detail

public function afterSave($insert, $changedAttributes)
{
    parent::afterSave($insert, $changedAttributes);

    // your code here
    Yii::$app->db->createCommand(sprintf("UPDATE room_types SET total_booked = total_booked + 1 WHERE room_type = '%s'", $this->room_type))->execute();

}

douyonglang4845
douyonglang4845 仍然没有更新数据库中的值
4 年多之前 回复
duancong2965
duancong2965 public function afterSave($ insert,$ changedAttributes){parent :: afterSave($ insert,$ changedAttributes); \ Yii :: $ app-> db-> createCommand(“UPDATE room_types SET total_booked = total_booked + 1 WHERE room_type ='$ model-> room_type'”) - > execute(); 我把它放在模型中
4 年多之前 回复
duanji8615
duanji8615 在changedAttributes中重命名changeAttributes
4 年多之前 回复
dongyun4010
dongyun4010 获取错误未定义的变量:changeAttributes
4 年多之前 回复
dppi5167
dppi5167 我编辑了答案
4 年多之前 回复
doudizhi947129
doudizhi947129 你能说明如何覆盖afterSave方法吗?
4 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐