Looping through array[$i] will not set object variable, but calling array[0] works

Hello everyone!

I have an array that is being generated via ::find() but when I try to loop through it to change information in one of it's variables, it fails unless I use a static number. Code example:

 $escalation = Escalations::find();
  for($i = 0; $i < sizeof($escalation); $i++) {
      $escalation[$i]->TIME_OPENED = 'test';
  }
  $this->view->setVar('escalation', $escalation);

This fails on the Volt side of things and has no information in TIME_OPENED.

However, if I use static numbers to assign TIME_OPENED, it works just fine. Example:

 $escalation = Escalations::find();
  $escalation[0]->TIME_OPENED = 'test0';
  $escalation[1]->TIME_OPENED = 'test1';
  $escalation[2]->TIME_OPENED = 'test2';
  $this->view->setVar('escalation', $escalation);

Obviously this is an issue since there may be 1 object or 10 objects. Any ideas?

edited Nov '15

PHP error log also seems to show that the variable is set. It seems the issue may be inside of volt?

{% for item in escalation %}
  ...
  <td> {{ item.TIME_OPENED }} </td>
  ...           
{% endfor %}
edited Nov '15

Hi not sure if this can help but have you tried using the method Escalations::count() in the model then populate the number in the for? I am assuming you just want the number of records in the whole table. I think find() returns a resultset so you might need to do ->toArray(); at the end before you can do sizeof.

You can have a look here: https://docs.phalconphp.com/en/latest/reference/models.html

edited Nov '15

Outputting to the error_log:

error_log("Size of \$escalation: " . sizeof($escalation));

error_log contents:

[Wed Nov 04 12:12:59.884742 2015] [:error] [pid 1806] [client xx.xx.xx.xx:63110] Size of $escalation: 3



6.6k
edited Nov '15

Make sure your array has one dimension(var_dump($escalation)).

I would suggest you to work with Model::find() return value as with resultset, which is it (unless you've played with hydration mode). E.g. use $escalation -> count() to get number of results and so on. Or you can use phalcon's toArray() method and then flatten array.