2013-03-07 28 views
1

错误:paginator的元素是对象(不是数组)。 var_dump($records)Zend Paginator +表网关:错误

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 } 

控制器:

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; 
     } 

在View:

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

我在做什么错?我怎样才能让$records作为一个数组?

预先感谢您!

回答

0

无论是从结果使用其toArray方法设置返回数组...

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

或者在你看来,使用toArray方法在你的foreach

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

其他信息: 的Zend \ Db \ ResultSet \ Exception \ RuntimeException File: /opt/lampp/htdocs/guest-book/vendor/zendframework/zendframework/library/Z end/Db/ResultSet/AbstractResultSet.php:265 消息: 作为此DataSource的一部分,行类型为object的行不能转换为数组 – Igor 2013-03-07 12:57:53

+0

Ah。你的模型是否有'getArrayCopy()'方法? 'public function getArrayCopy() { return get_object_vars($ this); }' – Crisp 2013-03-07 13:10:59

+0

是的,它有..... – Igor 2013-03-07 13:16:07