2017-08-18 39 views
0

我想分頁yii2.but從多個表中分頁。 這裏是現場控制器的我的動作代碼:如何從多個查詢yii2分頁?

<? 
 
public function actionPagination_product(){ 
 
$query=Product::find(); 
 

 
$count=$query->count(); 
 

 
$pagination=new Pagination(['totalCount' => $count, 'defaultPageSize' => 1]); 
 

 
$models = $query->offset($pagination->offset) 
 
     ->limit($pagination->limit) 
 
     ->all(); 
 
\t 
 
\t  return $this->render('pagination_pro', [ 
 
     'models' => $models, 
 
     'pagination' => $pagination, 
 
    ]); 
 

 
} 
 

 

 
?>

,這裏是我的看法:

<?php 
 
    use yii\widgets\LinkPager; 
 
?> 
 
<?php foreach ($models as $model): ?> 
 

 
<div class="list-group"> 
 
    <a href="#" class="list-group-item active"> 
 
    <h4 class="list-group-item-heading"><?= $model->title; ?></h4> 
 
    <p class="list-group-item-text"><?= $model->id; ?></p> 
 
\t <p class="list-group-item-text"><?= $model->body; ?></p> 
 
    </a> 
 
</div> 
 

 
    
 
    <br/> 
 
<?php endforeach; ?> 
 
<?php 
 
    // display pagination 
 
    echo LinkPager::widget([ 
 
     'pagination' => $pagination, 
 
    ]); 
 
?>

這是在多個表和搜索我無法加入他們。

例如我想讀報告:: find()會在這個分頁

如何做到這一點?

+0

你也應該顯示'Product :: find();'方法 –

+0

我的意思是我想從其他表讀取。如何做到這一點的其他表? – areff

回答

0

我不知道我是否正確理解你的問題。但如果你的問題是「我不能加入他們」。你可以加入你想用類似的東西在你的控制器,顯示錶:

$model = Report::find() 
->join('INNER JOIN', 'otherModel', 'otherModel.report_id = report.id'); 
->all(); 

這樣,您將擁有所有你在變量$模型需要的數據,然後你可以做你想做任何事的。