0

我需要在这段代码中添加一个分页:

    $select = new \Zend\Db\Sql\Select ;
    $select->from('school');
    $select->join('school_parent','school.school_parent_id = school_parent.school_parent_id');
    $resultSet = $this->tableGateway->selectWith($select);
    return $resultSet;

我试过这个例子,但我不能用连接表来做。

任何人都可以帮助我吗?

4

8 回答 8

3
public function fetchAll($paginated=false)
{
    if($paginated) {
        // create a new Select object for the table album
        $select = $this->getallAlbum();


        // create a new result set based on the Album entity
        $resultSetPrototype = new ResultSet();
        $resultSetPrototype->setArrayObjectPrototype(new Album());
        // create a new pagination adapter object
        $paginatorAdapter = new DbSelect(
        // our configured select object
        $select,
        // the adapter to run it against
        $this->tableGateway->getAdapter(),
        // the result set to hydrate
        $resultSetPrototype
        );
        $paginator = new Paginator($paginatorAdapter);
        return $paginator;
    }
    $resultSet = $this->tableGateway->select();
    return $resultSet;
}
 function getallAlbum(){
    $sql = new Select();
    $sql->from('album')->columns(array('id', 'artist', 'title'))->join('albumcategory', 'album.catId = albumcategory.catId', array('catName' => 'catName'), Select::JOIN_LEFT);
    return $sql; 
}
于 2014-02-11T04:58:20.037 回答
2

分页应该适用于连接。这就是我所拥有的,类似于您提供的示例:

$select = new \Zend\Db\Sql\Select ;
$select->from('school');
$select->join('school_parent','school.school_parent_id = school_parent.school_parent_id');

return new \Zend\Paginator\Paginator(
    new \Zend\Paginator\Adapter\DbSelect(
        $select,
        $this->tableGateway->getAdapter()
    )
);

如果上述方法不起作用,请发布您遇到的确切错误。

于 2013-06-21T15:32:33.003 回答
0

请尝试连接语法

$select->from('table1')->join('table2', 'table1 = table2', array('table2.colum_to_return1', 'table2.colum_to_return2'));
于 2014-02-24T06:24:03.103 回答
0

尝试另一个示例分页和排序

### module_config.php ###
'router' => array(
    'routes' => array(
        'album' => array(
            'type'    => 'segment',
            'options' => array(
                'route'    => '/album[/:action][/:id][/page/:page][/order_by/:order_by][/:order]',
                'constraints' => array(
                    'action' => '(?!\bpage\b)(?!\border_by\b)[a-zA-Z][a-zA-Z0-9_-]*',
                    'id'     => '[0-9]+',
                    'page' => '[0-9]+',
                    'order_by' => '[a-zA-Z][a-zA-Z0-9_-]*',
                    'order' => 'ASC|DESC',
                ),
                'defaults' => array(
                    'controller' => 'Album\Controller\Album',
                    'action'     => 'index',
                ),
            ),
        ),
    ),
),

###### AlbumController ###
namespace Album\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Album\Model\Album;
use Album\Form\AlbumForm;
use Zend\Db\Sql\Select;
use Zend\Paginator\Paginator;
use Zend\Paginator\Adapter\Iterator as paginatorIterator;

class AlbumController extends AbstractActionController {

protected $albumTable;

public function indexAction() {
    $select = new Select();

    $order_by = $this->params()->fromRoute('order_by') ?
            $this->params()->fromRoute('order_by') : 'id';
    $order = $this->params()->fromRoute('order') ?
            $this->params()->fromRoute('order') : Select::ORDER_ASCENDING;
    $page = $this->params()->fromRoute('page') ? (int) $this->params()->fromRoute('page') : 1;

    $albums = $this->getAlbumTable()->fetchAll($select->order($order_by . ' ' . $order));
    $itemsPerPage = 10;

    $albums->current();
    $paginator = new Paginator(new paginatorIterator($albums));
    $paginator->setCurrentPageNumber($page)
            ->setItemCountPerPage($itemsPerPage)
            ->setPageRange(7);

    return new ViewModel(array(
                'order_by' => $order_by,
                'order' => $order,
                'page' => $page,
                'paginator' => $paginator,
            ));
}
}
于 2014-02-24T06:29:54.837 回答
0

示例分页和排序 Url https://github.com/bigemployee/zf2-tutorial-paginate-sort

于 2014-02-24T06:56:51.987 回答
0

将此行添加到您的代码中:

$select->columns(array(new Expression('DISTINCT school.school_parent_id')));

或者是这样的:

$select->join('school_parent','school.school_parent_id = school_parent.school_parent_id', array('sp_id' => 'school_parent_id'));
于 2015-01-01T13:45:48.660 回答
0
// create a new Select object for the table album
        $select = new \Zend\Db\Sql\Select ;
        $select->from('school');
        $select->join('school_parent','school.school_parent_id = school_parent.school_parent_id');

//像下面这样改变

 // create a new Select object for the table album
    $select = $this->yourFunction();

//并定义yourFunction

 function youFunction(){
$sql = new Select();
$sql->from('school')->columns(array('schoolid', 'schoolname', 'anotherfield'))->join('school_parent', 'school.school_parent_id= school_parent.school_parent_id', array('schoolName' => 'schoolName'), Select::JOIN_LEFT);
return $sql; 

}

于 2014-02-21T03:52:30.287 回答
0

感谢您的回答,但我尝试了您的代码,但我收到了此 PDOException 消息:

SQLSTATE [42S21]:列已存在:1060 列名重复“school_parent_id”

我还尝试使用 Rob Allen 的示例调整我的“连接表”:

public function fetchAll($paginated=false)
     {
          if($paginated) {
        // create a new Select object for the table album
            $select = new \Zend\Db\Sql\Select ;
            $select->from('school');
            $select->join('school_parent','school.school_parent_id = school_parent.school_parent_id');
            // create a new result set based on the Album entity
            $resultSetPrototype = new ResultSet();
            $resultSetPrototype->setArrayObjectPrototype(new School());
            // create a new pagination adapter object
            $paginatorAdapter = new DbSelect(
            // our configured select object
            $select,
            // the adapter to run it against
            $this->tableGateway->getAdapter(),
            // the result set to hydrate
            $resultSetPrototype
            );
            $paginator = new Paginator($paginatorAdapter);
            return $paginator;
        }

        $resultSet = $this->tableGateway->select();
        return $resultSet;    
     }

但结果是一样的。

于 2013-07-01T10:11:50.503 回答