1

Error: elements of paginator are objects (not arrays). var_dump($records):

object(Records\Model\Records)#240 (11) { ["id"]=> NULL ["name"]=> string(9) "5453gdfgd" ["email"]=> string(16) "[email protected]" ["homepage"]=> string(0) "" ["text"]=> string(5) "ghjkj" ["image"]=> string(5) "Array" ["file"]=> string(0) "" ["ip"]=> NULL ["browser"]=> NULL ["date"]=> string(19) "2013-03-05 23:24:49" ["inputFilter":protected]=> NULL }  
object(Records\Model\Records)#241 (11) { ["id"]=> NULL ["name"]=> string(9) "5453gdfgd" ["email"]=> string(16) "[email protected]" ["homepage"]=> string(0) "" ["text"]=> string(5) "ghjkj" ["image"]=> string(5) "Array" ["file"]=> string(0) "" ["ip"]=> NULL ["browser"]=> NULL ["date"]=> string(19) "2013-03-05 23:23:37" ["inputFilter":protected]=> NULL }

Controller:

    protected $recordsTable;
        public function indexAction()
        {
            $field = (string) $this->params()->fromRoute('field', 'date');
            $order = (string) $this->params()->fromRoute('order', 'desc');
            $array = $this->getRecordsTable()->fetchAll($field, $order);

            $paginator = new Paginator\Paginator(new Paginator\Adapter\Iterator($array));
            $paginator->setCurrentPageNumber($this->params()->fromRoute('page', 1));
            $paginator->setItemCountPerPage(2);
            //print_r($paginator);
            $vm = new ViewModel(array('records' => $paginator));
            return $vm;
        }

    public function getRecordsTable()
        {
            if (!$this->recordsTable) {
                $sm = $this->getServiceLocator();
                $this->recordsTable = $sm->get('Records\Model\RecordsTable');
            }
            return $this->recordsTable;
        } 

RecordsTable:

    protected $tableGateway;

        public function __construct(TableGateway $tableGateway)
        {
            $this->tableGateway = $tableGateway;
        }

        public function fetchAll($field, $order)
        {
            $this->field = $field;
            $this->order = $order;
            $resultSet = $this->tableGateway->select(function (Select $select) {
            $select->columns(array('date', 'name', 'email', 'homepage', 'text', 'image', 'file'));
            $select->order($this->field.' '.$this->order);        
            });
            $resultSet->buffer();
            $resultSet->next();

            return $resultSet;
        }

In View:

 foreach($records as $record) : ?>
     <?php var_dump($record); ?> <br />
 <?php endforeach; ?>

What am I doing wrong? How can I make $records as an array?

Thank you in advance!

1 Answer 1

0

Either return an array from the result set using its toArray method...

public function fetchAll($field, $order)
{
     // ...
     return $resultSet->toArray();
}

Or in your view, use the toArray method in your foreach

<?php foreach($records->toArray() as $record) : ?>
    <?php var_dump($record); ?> <br />
<?php endforeach;      
6
  • Additional information: Zend\Db\ResultSet\Exception\RuntimeException File: /opt/lampp/htdocs/guest-book/vendor/zendframework/zendframework/library/Zend/Db/ResultSet/AbstractResultSet.php:265 Message: Rows as part of this DataSource, with type object cannot be cast to an array
    – Igor
    Mar 7, 2013 at 12:57
  • Ah. Does your model have a 'getArrayCopy()` method? public function getArrayCopy() { return get_object_vars($this); }
    – Crisp
    Mar 7, 2013 at 13:10
  • K, how about a toArray() method (same functionality)? Think the getArrayCopy method only gets used if the model is an ArrayObject instance (at least as far as results sets go).
    – Crisp
    Mar 7, 2013 at 13:37
  • Thanks for help. What am I have to do? As we see, model is an ArrayObject instanceю
    – Igor
    Mar 7, 2013 at 14:12
  • Did you implement the toArray() method as suggested? The error you see comes from the resultsets toArray method trying to call the toArray or getArrayCopy methods of your model object, and failing, so if you've implemented both, it should be working with at least one of them.
    – Crisp
    Mar 7, 2013 at 14:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.