bravik
bravik

Reputation: 460

Yii2 json response converts arrays to JSON objects, not json arrays

I have a yii2 model and relations.
Yii2 response format set to JSON.
I want to return model with its relationships (one-to-many) and so far everything was fine.

However, in this case for one of model relationships JSON response encodes into JSON object {}, not array []. And I can't figure out why exactly? For other relationships it is an array.

Below is var_dump of a model. The problematic relations is items

/backend/controllers/TasksController.php:181:
object(app\models\Task)[163]
  private '_attributes' (yii\db\BaseActiveRecord) => 
    array (size=6)
      'id' => int 9
      'title' => string 'Sample dnewsd' (length=13)
      'mode' => string 'onlyads' (length=7)
      'deployed' => int 0
      'start_time' => string '2017-11-16 00:00:00' (length=19)
      'end_time' => null
  private '_oldAttributes' (yii\db\BaseActiveRecord) => 
    array (size=6)
      'id' => int 9
      'title' => string 'Sample dnewsd' (length=13)
      'mode' => string 'onlyads' (length=7)
      'deployed' => int 0
      'start_time' => string '2017-11-16 00:00:00' (length=19)
      'end_time' => null
  private '_related' (yii\db\BaseActiveRecord) => 
    array (size=3)
      'items' => 
        array (size=2)
          0 => 
            object(app\models\TaskItem)[178]
              ...
          2 => 
            object(app\models\TaskItem)[201]
              ...
      'devices' => 
        array (size=0)
          empty
      'deviceGroups' => 
        array (size=0)
          empty

Here is the JSON return when I edit model:

{
    "status": "ok",
    "data": {
        "id": 9,
        "title": "Sample task",
        "mode": "onlyads",
        "deployed": 0,
        "start_time": "2017-11-16 00:00:00",
        "end_time": null,
        "items": {
            "0": {
                "id": 2,
                "image_url": "/media/lightboxes/pictures/ad_.jpg",
                "duration": 20,
                "task_id": 9
            },
            "2": {
                "id": 46,
                "image_url": "/media/lightboxes/pictures/ad_46.jpg",
                "duration": 20,
                "task_id": 9
            }
        },
        "devices": [],
        "deviceGroups": []
    }
}

You can see that items is not a JSON array: items : {}

In another action - GET the same model the returned JSON has items: []:

{
    "status": "ok",
    "data": {
        "id": 9,
        "title": "Sample dnewsd",
        "mode": "onlyads",
        "deployed": 0,
        "start_time": "2017-11-16 00:00:00",
        "end_time": null,
        "items": [
            {
                "id": 2,
                "image_url": "/media/lightboxes/pictures/ad_.jpg",
                "duration": 20,
                "task_id": 9
            },
            {
                "id": 46,
                "image_url": "/media/lightboxes/pictures/ad_46.jpg",
                "duration": 20,
                "task_id": 9
            }
        ],
        "devices": [],
        "deviceGroups": []
    }
}

Here is var_dump for the same model in GET action:

/srv/www/erp-ang/backend/controllers/TasksController.php:55:
object(app\models\Task)[163]
  private '_attributes' (yii\db\BaseActiveRecord) => 
    array (size=6)
      'id' => int 9
      'title' => string 'Sample dnewsd' (length=13)
      'mode' => string 'onlyads' (length=7)
      'deployed' => int 0
      'start_time' => string '2017-11-16 00:00:00' (length=19)
      'end_time' => null
  private '_oldAttributes' (yii\db\BaseActiveRecord) => 
    array (size=6)
      'id' => int 9
      'title' => string 'Sample dnewsd' (length=13)
      'mode' => string 'onlyads' (length=7)
      'deployed' => int 0
      'start_time' => string '2017-11-16 00:00:00' (length=19)
      'end_time' => null
  private '_related' (yii\db\BaseActiveRecord) => 
    array (size=3)
      'items' => 
        array (size=2)
          0 => 
            object(app\models\TaskItem)[178]
              ...
          1 => 
            object(app\models\TaskItem)[184]
              ...
      'devices' => 
        array (size=0)
          empty
      'deviceGroups' => 
        array (size=0)
          empty

Any clue why could this happen?

Upvotes: 1

Views: 1332

Answers (1)

csminb
csminb

Reputation: 2382

arrays need to have consecutive indexes starting from 0,
if you need to re-index an already loaded relation you can run

$task->populateRelation('items', array_values($task->items));

Upvotes: 1

Related Questions