简体   繁体   English

在 Yii2 中,我传递了 model 值,但它显示为 null

[英]In Yii2 i has passing model value but its showing as a null

There is my controller code有我的 controller 代码

 if ($this->request->isPost) {
            $model->created_by = Yii::$app->user->identity->id;
            $model->created_at = date('Y/m/d');
            // echo $model->created_at;die;
            if ($model->load($this->request->post()) && $model->save()) {
                return $this->redirect(['index', 'id' => $model->id]);
            }
        } 

and there is my model rule还有我的 model 规则

public function rules()
{
    return [
        [['deduction_type'], 'required'],
        [['created_at'], 'safe'],
        [['created_by'], 'integer'],
        [['deduction_type'], 'string', 'max' => 100],
    ];
}

My problem is a every time i pass the value in create_at and create_by that is save in databasde as a null in want my value in db我的问题是每次我传递 create_at 和 create_by 中的值时,它在数据库中保存为 null 想要我在 db 中的值

Instead of your way而不是你的方式

 if ($this->request->isPost) {
            //Move that two Lines inside the if
            $model->created_by = Yii::$app->user->identity->id;
            $model->created_at = date('Y/m/d');

            // echo $model->created_at;die;
            if ($model->load($this->request->post()) && $model->save()) {
                return $this->redirect(['index', 'id' => $model->id]);
            }
        }

I usually do the following:我通常会做以下事情:

 if ($this->request->isPost) {
            if ($model->load($this->request->post()) && $model->validate()) {
                $model->created_by = Yii::$app->user->identity->id;
                $model->created_at = date('Y/m/d');
                $model->save();
                return $this->redirect(['index', 'id' => $model->id]);
            }
        }

validate() ->Checks if the Inputs are Correct according to your rules. validate() -> 根据您的规则检查输入是否正确。 Afterwards you know that the entries were correct and you can set your values.之后您知道条目是正确的并且您可以设置您的值。
This is my usual way of tackling this problem.这是我解决这个问题的常用方法。 You can also wrap $model->save();你也可以包装$model->save(); with an if to check your changes as well and to catch the potential false of save().使用if来检查您的更改并捕获 save() 的潜在false

Check your POST, is it send empty filds created_at and created_by?检查您的 POST,它是否发送空字段 created_at 和 created_by? don't send this fields in post and load() method will not replace it on null values.不要在 post 中发送此字段,并且 load() 方法不会在 null 值上替换它。

If you are really want to insert current user id and current time, use the BlameableBehavior and TimestampBehavior.如果您真的想插入当前用户 ID 和当前时间,请使用 BlameableBehavior 和 TimestampBehavior。

BlameableBehavior automatically fills the specified attributes with the current user ID. BlameableBehavior 自动使用当前用户 ID 填充指定的属性。 https://www.yiiframework.com/doc/api/2.0/yii-behaviors-blameablebehavior https://www.yiiframework.com/doc/api/2.0/yii-behaviors-blameablebehavior

TimestampBehavior automatically fills the specified attributes with the current timestamp. TimestampBehavior 自动用当前时间戳填充指定的属性。 https://www.yiiframework.com/doc/api/2.0/yii-behaviors-timestampbehavior https://www.yiiframework.com/doc/api/2.0/yii-behaviors-timestampbehavior

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM