ResultSet in Zend Framework 2 doesn't work with toArray ()

I thought I could use toArray()

Zend in my result set, but I find that using toArray()

with the message gives the message:

Strings as part of this DataSource, with an object of type cannot be passed into an array

What I thought would work like

return new JsonModel($collections->toArray());

But it fails with the above error message.

Here is a collection, a small class

class Collection
{
public $collectionID;
public $name;

public function exchangeArray($data)
{
    $this->collectionID = (!empty($data['collectionID'])) ? $data['collectionID'] : null;
    $this->name = (!empty($data['name'])) ? $data['name'] : null;
}

// Add the following method:
public function getArrayCopy()
{
    return get_object_vars($this);
}


}

      

If I add mine

public function toArray()
{
  return array(get_object_vars($this));
}

      

I can get it to do what I expect, but I'm not sure if this is the best approach. Also if I use this in conjunction with JsonModel

, the JSON output will also contain the variables from settings.global.php

Thanks in advance,

+3


source to share


2 answers


You can use $result->getArrayCopy()

if it doesn't work, you can try adding to Collection this function



 public function getArrayCopy()
 {
     return get_object_vars($this);
 }

      

0


source


You need to iterate over the records as they won't get everything in one go. So something like:

$m = array();

foreach($resultSet as $r)
    $m[] = (array)$r;

      



Or try:

$resultSet = (array)resultSet;

      

0


source







All Articles