I have array of plain PHP object that I want to use in conjunction with Zend_Paginator and partialLoop

219 views Asked by At

I am implementing DAO pattern in my sample app and I have plain array that contains User(domain) fetched from UserMapper I want to use Zend_Paginator with array adapter, but it does not work it only works when I use Zend_DbTable adapter which I dont want to do because it defeats the purpose of DAO.

sample code below (Not Working)

    $userMapper = new Application_Model_UserMapper();
    $users = $userMapper->getUsers();
    $paginator = Zend_Paginator::factory($users);
    $paginator->setCurrentPageNumber($this->_getParam('page'));
    $paginator->setItemCountPerPage(1);
    $this->view->paginator = $paginator;

sample code below (Working)

    $users = new Application_Model_DbTable_User();
    $select = $users->fetchAll();
    $paginator = Zend_Paginator::factory($select);
    $paginator->setCurrentPageNumber($this->_getParam('page'));
    $paginator->setItemCountPerPage(1);
    $this->view->paginator = $paginator;
2

There are 2 answers

0
Sharlon M. Balbalosa On

I already solve my problem, in order for array containing plain PHP objects to be recognized by the partialLoop you need to implement a toArray() method in that class and return key value pair of the attributes

  class Application_Model_User
  {
  private $id;
  private $first_name;
  private $last_name;
  private $middle_name;

  public function toArray()
  {
   return get_object_vars($this);
  }
  }
0
RockyFord On

I was looking at the factory method and it takes 3 parameters

public static function factory($data, $adapter = self::INTERNAL_ADAPTER,
                               array $prefixPaths = null)

you may want to try

$paginator = Zend_Paginator::factory($users, 'Array');

at least this way if your data is somehow incorrect you should raise an exception.