mirror of
https://github.com/yiisoft/yii2.git
synced 2025-11-03 05:48:11 +08:00
Fix #17886: yii\rest\Serializer serialize arrays
This commit is contained in:
committed by
Alexander Makarov
parent
110199bcfd
commit
ecbafa2e3a
@ -6,6 +6,7 @@ Yii Framework 2 Change Log
|
||||
|
||||
- Bug #17859: Fix loading fixtures under Windows (samdark)
|
||||
- Bug #11945: Fix Schema Builder MySQL column definition order (simialbi)
|
||||
- Bug #17886: yii\rest\Serializer serialize arrays (patacca)
|
||||
- Bug #16334: Serializer support `\JsonSerializable` (germanow)
|
||||
- Bug #17798: Avoid creating folder for stream log targets in `FileTarget` (wapmorgan)
|
||||
- Bug #17850: Update to `ReplaceArrayValue` config exception message (alex-code)
|
||||
|
||||
@ -152,6 +152,12 @@ class Serializer extends Component
|
||||
return $this->serializeModel($data);
|
||||
} elseif ($data instanceof DataProviderInterface) {
|
||||
return $this->serializeDataProvider($data);
|
||||
} elseif (is_array($data)) {
|
||||
$serializedArray = [];
|
||||
foreach ($data as $key => $value) {
|
||||
$serializedArray[$key] = $this->serialize($value);
|
||||
}
|
||||
return $serializedArray;
|
||||
}
|
||||
|
||||
return $data;
|
||||
|
||||
@ -442,6 +442,32 @@ class SerializerTest extends TestCase
|
||||
],
|
||||
$serializer->serialize($model));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see https://github.com/yiisoft/yii2/issues/17886
|
||||
*/
|
||||
public function testSerializeArray()
|
||||
{
|
||||
$serializer = new Serializer();
|
||||
$model1 = new TestModel();
|
||||
$model2 = new TestModel();
|
||||
$model3 = new TestModel();
|
||||
|
||||
$this->assertSame([
|
||||
[
|
||||
'field1' => 'test',
|
||||
'field2' => 2,
|
||||
],
|
||||
[
|
||||
'field1' => 'test',
|
||||
'field2' => 2,
|
||||
],
|
||||
'testKey' => [
|
||||
'field1' => 'test',
|
||||
'field2' => 2,
|
||||
],
|
||||
], $serializer->serialize([$model1, $model2, 'testKey' => $model3]));
|
||||
}
|
||||
}
|
||||
|
||||
class TestModel extends Model
|
||||
|
||||
Reference in New Issue
Block a user