2013-06-20 75 views
0

我需要在此代碼添加分頁:的Zend框架2分頁表連接

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

我試過this example,但我不能與連接表做到這一點。

任何人都可以幫到我嗎?

+0

究竟是什麼問題? – Ruben

+0

你好,我需要添加一個連接表的分頁,但我有這個PDOException消息: SQLSTATE [42S21]:列已存在:1060重複列名'school_parent_id' –

回答

1

分頁應該可以在連接時正常工作。這就是我,類似的例子,你提供:

$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() 
    ) 
); 

請張貼如果上述不工作,你得到一個確切的錯誤。

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

但是結果是一樣的。

+0

它可能是一個錯誤,請參閱:https: //github.com/zendframework/zf2/issues/4719 – Ruben

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

}

0

請嘗試連接語法

$select->from('table1')->join('table2', 'table1 = table2', array('table2.colum_to_return1', 'table2.colum_to_return2')); 
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, 
      )); 
} 
} 
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'));