In general, if you have a toArray() method, it is generally accepted that the return value of that method will be a PHP type array.

If you are wanting to get the objects in your view, simply iterate over the ResultSet. The default return type in the default ResultSet is a RowObject which is actually an extension of ArrayObject.

foreach ($resultSet as $row) {}

as opposed to:

foreach ($resultSet->toArray() as $row) {}

-ralph

On 3/26/12 2:35 AM, duke wrote:
why in this function we transform data instance of RowObjectInterface to
array? Not only ResultSet object? That we remove advantage of object access
in View (like echo $row->id).


code:
     /**
      * Cast result set to array of arrays
      *
      * @return array
      * @throws Exception\RuntimeException if any row is not castable to an
array
      */
     public function toArray()
     {
         $return = array();
         foreach ($this as $row) {
             if (is_array($row)) {
                 $return[] = $row;
             } elseif (method_exists($row, 'toArray')) {
                 $return[] = $row->toArray();
             } elseif ($row instanceof ArrayObject) {
                 $return[] = $row->getArrayCopy();
             } else {
                 throw new Exception\RuntimeException('Rows as part of this
datasource cannot be cast to an array');
             }
         }
         return $return;
     }


I guess it should be loop without transforming RowObject to array access
only:

     public function toArray()
     {
         $return = array();
         foreach ($this as $row) {
             if ($row instanceof RowObjectInterface) {
                 $return[] = $row;
             } elseif (method_exists($row, 'toArray')) {
                 $return[] = $row->toArray();
             } else {
                 throw new Exception\RuntimeException('Rows as part of this
datasource cannot be cast to an array');
             }
         }
         return $return;
     }


I tested, it don't emit "mysql out of sync error" too. Otherwise we should
create additional function to avoid  "mysql out of sync error".

--
View this message in context: 
http://zend-framework-community.634137.n4.nabble.com/ZF2-Db-ResultSet-toArray-algoritm-tp4505005p4505005.html
Sent from the Zend Framework mailing list archive at Nabble.com.



--
List: [email protected]
Info: http://framework.zend.com/archives
Unsubscribe: [email protected]


Reply via email to