If I am not misunderstand, you are trying fetch current datetime from database, but it generates an instance of Express, not a current datetime value, you should execute your query:
$expression = new Expression('NOW()');
$now = (new \yii\db\Query)->select($expression)->scalar(); // SELECT NOW();
$this->field_date = $now;
But I think it is not the right way to do that!
Firstly, it is no need to retrieve current time from database, use PHP instead to avoid increasing database overhead.
Secondly, the time could be affected by the database’s time zone, it will causes some problems if the PHP zone time is different with database time zone.
It is recommended that always use PHP to retrieve datetime, we just need to concern about PHP zone time:
Expression(“now()”) is used to save the current db datetime into the field. Didn’t use PHP due to timezone issues, as timezone is set based on user’s location.
So there’s no way to define validation to allow both datetime string and Expression(“now()”)?
Why dont use extension like kartik datecontrol ? Then you can save the datetime in GMT and display it according to user’s timezone.
And, does Expression(‘NOW()’) has to be the part of yii\db\Query? I dont understand why the field will have value of dbexpression? Will it be stored as the current datetime when the model is saved? Is it similar as default value in mysql table definition?