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,
source to share