2016-09-12 12 views
0

アプリケーションでは、Mapperを実装して、ResultSetからデータベースとオブジェクトからデータを取得しました。私はすべてのデータを取得するためにはJOIN複数のテーブル。それから私は、例えば、オブジェクト(MainFooBarなど)を構築し、配列を反復処理し、それらを組み合わせZend Framework 2でJOINを使用してページネーションを使用する方法は?

[ 
    0 => [ 
     main_id => 1, 
     main_title => 'lorem', 
     main_desc => 'ipsum', 
     foo_id => '12', 
     foo_name => 'qwer', 
     bar_id => '56', 
     bar_name => 'asdf' 
    ], 
    1 => [ 
     main_id => 1, 
     main_title => 'lorem', 
     main_desc => 'ipsum', 
     foo_id => '12', 
     foo_name => 'qwer', 
     bar_id => '67', 
     bar_name => 'hjkl' 
    ], 
    2 => [ 
     main_id => 1, 
     main_title => 'lorem', 
     main_desc => 'ipsum', 
     foo_id => '23', 
     foo_name => 'uiop', 
     bar_id => '67', 
     bar_name => 'hjkl' 
    ], 
    ... 
    10 => [ 
     main_id => 1, 
     main_title => 'lorem', 
     main_desc => 'ipsum', 
     foo_id => '23', 
     foo_name => 'uiop', 
     bar_id => '91', 
     bar_name => 'cvbn' 
    ], 
    11 => [ 
     main_id => 2, 
     main_title => 'dolor', 
     main_desc => 'sit', 
     foo_id => '78', 
     foo_name => 'fghj', 
     bar_id => '89', 
     bar_name => 'vbnm' 
    ], 
    ... 
    12 => [ 
     main_id => 3, 
     foo_id => '135', 
     bar_id => '246', 
     ... 
    ], 
    13 => [ 
     main_id => 3, 
     foo_id => '135', 
     bar_id => '468', 
     ... 
    ], 
    14 => [ 
     main_id => 3, 
     foo_id => '357', 
     bar_id => '680', 
     ... 
    ], 
    ... 
    1000 => [ 
     ... 
    ] 
] 

:それから私は、このような構造の巨大な配列を取得します

$newMain = $myMainHydrator->hydrate($results[0]); 
$newFoo = $myFooHydrator->hydrate($results[0]); 
$newBar = $myBarHydrator->hydrate($results[0]); 
$newFoo->setBar($newBar); 
$newMain->setFoo($newFoo); 
$resultObjects[] = $newMain; 

は、今私はPaginatorに建設され、次の問題を得た:PaginatorLIMIT、例えば設定します10を検索し、10の行だけを取得しますが、すべてのオブジェクトに複数の結果行が必要です(現在も12行)。

私は信じられない、PaginatorJOINを処理できないので、動作させる方法が必要です。複合体SELECTPaginatorの使用方法はJOINとなりますか?

答えて

1

問題がPaginatorAdaptergetItems(...)INによってLIMIT句を置換することによって解決することができます。

namespace Foo\Paginator\Adapter; 

use Zend\Db\Sql\Select; 
use Zend\Paginator\Adapter\DbSelect; 
use Zend\Db\Sql\Sql; 
use Zend\Db\Sql\Expression; 

class FooPaginatorAdapter extends DbSelect 
{ 

    public function count() 
    { 
     $select = new Select(); 
     $select->from('foo')->columns([self::ROW_COUNT_COLUMN_NAME => new Expression('COUNT(*)')]); 

     $statement = $this->sql->prepareStatementForSqlObject($select); 
     $result = $statement->execute(); 
     $row  = $result->current(); 
     $this->rowCount = $row[self::ROW_COUNT_COLUMN_NAME]; 

     return $this->rowCount; 
    } 

    public function getItems($offset, $itemCountPerPage) 
    { 
     $select = clone $this->select; 
     // replaced 
     // $select->offset($offset); 
     // $select->limit($itemCountPerPage); 
     // by this 
     $relevantIds = $this->getRelevantIds($offset, $itemCountPerPage); 
     $select->where->in('foo.id', $relevantIds); 

     $statement = $this->sql->prepareStatementForSqlObject($select); 
     $result = $statement->execute(); 

     $resultSet = clone $this->resultSetPrototype; 
     $resultSet->initialize($result); 

     return iterator_to_array($resultSet); 
    } 

    protected function getRelevantIds($offset, $itemCountPerPage) 
    { 
     $sql = new Sql($this->sql->getAdapter()); 
     $select = $sql->select('foo'); 
     $select->columns(['id']); 
     $select->offset($offset); 
     $select->limit($itemCountPerPage); 

     $statement = $this->sql->prepareStatementForSqlObject($select); 
     $result = $statement->execute(); 

     $resultArray = iterator_to_array($result); 

     $relevantIds = array_column($resultArray, 'id'); 

     return $relevantIds; 
    } 

} 

あなたはこのケースで見たようID初の独立したデータベースリクエストで取得する必要があります。しかし、とにかく - それは動作します。より良い解決策がわかっている場合は、気軽に投稿してください。

関連する問題