0

I got a ZF2 project with 2 Models PosTable/TopTable which extend AbstractTableGateway.

I want to Paginate results from those Tables so i have a Pagination function in both of them.

this is what the PosTable Model looks like:

...
class PosTable extends AbstractTableGateway {

public function __construct($adapter) {
    $this->table = 'pos';
    $this->adapter = $adapter;
}
...
public function getPosPaginator($tid) {
    $sql = $this->getSql();
    $select = $sql->select();
    $select->where('tid = '.$tid)->where('deleted = 0')->order('crdate ASC');
    $adapter = new \Zend\Paginator\Adapter\DbSelect($select, $sql);
    $paginator = new \Zend\Paginator\Paginator($adapter);
    return $paginator;
}
...

which works perfectly. but in my TopTable it looks the same like this:

...
class TopTable extends AbstractTableGateway {

public function __construct($adapter) {
    $this->table = 'top';
    $this->adapter = $adapter;
}

public function getTopPaginator($fid) {
    $sql = $this->getSql();
    $select = $sql->select();
    $select->where('fid = '.$fid)->where('deleted = 0');
    $adapter = new \Zend\Paginator\Adapter\DbSelect($select, $sql);
    $paginator = new \Zend\Paginator\Paginator($adapter);
    return $paginator;
}

 ...

my controller looks like this for PosTable:

  ...       
  public function posAction(){
            ...
    $pos = $this->getPosTable()->getPosPaginator($tid);

    $pos->setCurrentPageNumber($pageid)->setItemCountPerPage(19);
            ... return $pos etc...

same controller topAction:

    ...
public function topAction(){
            ...
        $top = $this->getTopTable()->getTopPaginator($fid);
    $top->setCurrentPageNumber($pageid)->setItemCountPerPage(20);
            ...return $top etc..

in that controller i got also these functions:

    public function getTopTable(){
    return $this->getServiceLocator()->get('Application\Model\TopTable');
}
public function getPosTable(){
    return $this->getServiceLocator()->get('Application\Model\PosTable');
}

PosTable Pagination works perfectly, but the TopTable Pagination doesnt work. i get this error:

Fatal error: Call to a member function select() on a non-object in ....

seems like

        $sql = $this->getSql();

doesnt return the object. how can i solve this problem? one works one doesnt for no obvious reason.

my module.php looks like this:

    namespace Application;

class Module
{
    public function getAutoloaderConfig()
    {
        return array('Zend\Loader\StandardAutoloader' =>
                array('namespaces' =>
                        array(__NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,),
                ),
        );
    }

    public function getConfig()
    {
        return include __DIR__ . '/config/module.config.php';
    }

    public function getServiceConfig()
    {
        return array(
                'factories' => array(


'Application\Model\TopTable' =>  function($sm) {
                        $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
                        $table     = new \Application\Model\TopTable($dbAdapter);
                        return $table;
                    },
                    'Application\Model\ForTable' =>  function($sm) {
                        $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
                        $table     = new \Application\Model\ForTable($dbAdapter);
                        return $table;
                    },
                    'Application\Model\PosTable' =>  function($sm) {
                        $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');


                        $table     = new \Application\Model\PosTable($dbAdapter);
                            return $table;
                        },
                ),
        );
    }
}

1 Answers1

1

Ok, after going through TableGateway Code and your extended class, I found the your implementation is not calling initialize which setup the sql object try to call the parent table gateway, below is the modified constructor for your PosTable and TopTable

    /* for PosTable */
class PosTable extends TableGateway {

    public function __construct($adapter) {
        parent::__construct('pos', $adapter);
    }
...

    /* for TopTable */
class TopTable extends TableGateway {

    public function __construct($adapter) {
        parent::__construct('top', $adapter);
    }
...
Raj
  • 1,156
  • 11
  • 15
  • thanks so much for your effort! but i still cant get it to work. i changed TopTable Model like this: public function __construct($adapter) { parent::__construct('top',$adapter); /*$this->table = 'top'; $this->adapter = $adapter;*/ } now i get this error: Fatal error: Cannot call constructor in ..... – José Marquez Dec 29 '12 at 16:13
  • could you please add all the error message trace, are you able identify anything from the error log – Raj Dec 29 '12 at 16:22
  • sadly there is no error trace shown, only "Fatal error: Cannot call constructor in C:\...\module\Application\src\Application\Model\TopTable.php on line 12" php error log only has the same error msg (PHP Fatal error) as quoted. – José Marquez Dec 29 '12 at 16:33
  • Sorry I just noticed you had extended AbstractTableGateway which doesn't have the constructor and initialize code, try extend with TableGateway which has the constructor and init code – Raj Dec 29 '12 at 16:42
  • i think i might have solved it with a workaround: i replaced $sql = $this->getSql(); with $sql = new Sql($this->adapter, $this->table); so a new sql object is created and the function works as expected. thanks for your help, idea came after checking the TableGateway constructor like you suggested – José Marquez Dec 29 '12 at 16:43
  • nice you have the solution, I thought of recommending the same earlier, but since you had extended the base class it would be appropriate to call the parent init code and using the default sql, I had modified the code in my answer and IMHO it's recommended to do extend the table gateway and call its constructor with the table name of your choice – Raj Dec 29 '12 at 16:55