Hello! I am new to Yii framework, so I am not sure how to deal with this small problem I have been stuck on for more than a day.
So my problem is that I want to store some data in the database, but it fails on calling save(), and the only way I can get it to work is by disabling validation, but I want to do it with validation.
The error message I get from calling getErrors() is as in the title ‘Value must be a string’. So I am trying to typecast the necessary value through validation.
In the docs I found this - https://www.yiiframework.com/doc/api/2.0/yii-validators-stringvalidator#$strict-detail
So I try to use it through rules()
public function rules(): array { return ArrayHelper::merge( parent::rules(), [ ['value', 'string', 'strict' => false], ] ); }
This is the part where I try to store the data into the database
public function setHasTransitArrival($value = true) { $meta = MetaInformation::findOne([ 'remote_table' => self::tableName(), 'remote_id' => $this->id, 'field' => self::META_HAS_TRANSIT_ARRIVAL, ]); if ($meta === null) { $meta = new MetaInformation(); $meta->remote_table = self::tableName(); $meta->remote_id = $this->id; $meta->field = self::META_HAS_TRANSIT_ARRIVAL; } $meta->value = $value ? 1 : 0; $meta->validate(); $meta->save(); }
The way it is written now does not change anything.
I expected it to validate the $value variable and cast it to ‘string’, and by adding " ‘strict’ => false " I expect it to accept scalar values and convert them accordingly.
Could someone, please, point out what do I not understand correctly or what am I doing wrong?