Closed codeclass closed 11 months ago
It's because \yii\db\ActiveRecord
is Arrayable
. With current framework implementation you need to have your own Serializer (or at least overwritten serialize()
) to change the order of these if
conditions. Maybe we should introduce some customizable order? 🤔
Im using Controller that implements yii\rest\Controller and this serializer used by afterAction method. I can make my own serializer and point it at $serializer in my extended controller, but I think it is not nice.
May be possible to check, if model is JsonSerializable in "serializeModel" function and merge additional fields? https://github.com/yiisoft/yii2/blob/d43341a73ab9ab711c42b74c117e076275d9b8c0/framework/rest/Serializer.php#L261
Wouldn't it be hard to decide which fields are to be kept and which are to be overwritten and in what order?
What steps will reproduce the problem?
example model
then try to Serialize with yii\rest\Serializer
What is the expected result?
expected test field in dump
What do you get instead?
no test field in dump
Additional info
Model is serializing like Arrayable
https://github.com/yiisoft/yii2/blob/d43341a73ab9ab711c42b74c117e076275d9b8c0/framework/rest/Serializer.php#L149
it is JsonSerializable too, but this code is not running:
https://github.com/yiisoft/yii2/blob/d43341a73ab9ab711c42b74c117e076275d9b8c0/framework/rest/Serializer.php#L151