When Yii2 ActiveRecord save (), the non-date type field is set to Expression verification failure, is there a solution within the framework?

$model = new Test();
$model->uuid   = new Expression("UUID()");
$model->create = new Expression("NOW()");
if($model->save() === false)
{
    echo(json_encode($model->getErrors()));exit;
}
else
    echo("saved");

the output is shown as:

{"uuid ":["uuid must be a string."]}

the result of the check is that the validation rules automatically generated by the Yii2 framework

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [["create"], "safe"],
            [["uuid"], "string", "max" => 64],
            [["uuid"], "unique"],
        ];
    }
  • date type field is safe, only verify whether it exists, and do not do specific verification
  • The
  • string type field is verified by string, and it is found that Expression has reported an error
  • .

non-framework solutions include inheriting ActiveRecord processing to validate related code and skipping Expression

Is there any ready-made way to solve this problem within the

framework?

someone must have encountered a similar problem, and there should be a solution within the framework.

Feb.27,2021
Menu