Zend Framework 2 Abstract TableGateway getSql

时间:2012-12-28 15:01:21

标签: zend-framework2 zend-db zend-db-table zend-db-select

我得到了一个带有2个模型PosTable / TopTable的ZF2项目,它扩展了AbstractTableGateway。

我想从这些表中分析结果,所以我在这两个表中都有分页功能。

这就是PosTable模型的样子:

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

完美无缺。 但在我的TopTable中看起来像这样:

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

 ...

我的控制器对于PosTable来说是这样的:

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

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

相同的控制器topAction:

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

在那个控制器里我也得到了这些功能:

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

PosTable Pagination完美无缺,但TopTable分页无效。 我收到这个错误:

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

好像是

        $sql = $this->getSql();

不返回该对象。 我怎么解决这个问题? 一个人工作一个没有明显的原因。

我的module.php看起来像这样:

    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 个答案:

答案 0 :(得分:1)

好的,经过TableGateway Code和你的扩展类后,我发现你的实现没有调用initialize,sql对象尝试调用父表网关,下面是你的PosTable和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);
    }
...