Yii2-分页到ActiveRecord的联合查询
我有一个例子,我需要一个联合查询,同时将最终结果作为ActiveRecord。为了举例说明,我有两个表产品和类别。每个都有一个从ActiveRecord扩展而来的模型。我试图提出如下问题Yii2-分页到ActiveRecord的联合查询,yii,yii2,Yii,Yii2,我有一个例子,我需要一个联合查询,同时将最终结果作为ActiveRecord。为了举例说明,我有两个表产品和类别。每个都有一个从ActiveRecord扩展而来的模型。我试图提出如下问题 $deletedProducts = Products::find()->select([$productsTable . '.id', $productsTable . '.selling_price']) ->where(['client' => $clie
$deletedProducts = Products::find()->select([$productsTable . '.id', $productsTable . '.selling_price'])
->where(['client' => $clientId])
->andWhere(['<>', 'hidden', 0]);
然后通过ActiveDataProvider返回结果
$dataProvider = new ActiveDataProvider(
[
'query' => $archivedItemsQuery,
]
]);
return $dataProvider->getModels();
问题是,当我尝试在请求中发送分页数据,或通过ActiveDataProvider设置分页数据时,不会发生分页。当然,我可以将此查询更改为
$unionQuery = (new \yii\db\Query())
->from(['counter_items' => $archivedItemsQuery]);
这会导致正确的分页,但我不会得到ActiveRecords的结果
关于如何使用1创建联合查询的任何想法。分页工作和2。结果是ActiveRecord。您好,您可以尝试以下方法吗,虽然不是最简单的方法,但我希望它对您有效。我在我这边对它进行了测试,如果我理解正确,您希望得到
$dataProvider->getModels()的结果
返回ActiveRecord
Models对象,即Products
model,并希望GridView
分页同时正常工作
您需要更改ActiveDataProvider
以按以下方式使用union$archivedItemsQuery
$dataProvider = new \yii\data\ActiveDataProvider([
'query' => Products::find()->from(['products' => $archivedItemsQuery]),
'pagination' => [
'pageSize' => 10,
],
]);
如果您编写它,
$dataProvider=new\yii\data\ActiveDataProvider(['query'=>Products::find()->from(['Products'=>$archivedItemsQuery]),'pagination'=>['pageSize'=>8,],])代码>我认为有一种更整洁的方法,但这种方法非常有效。非常感谢:)我不确定,但你能试一下使用Products::findBySql($archivedItems Query->createCommand()->->rawSql)
对于Query
参数,我认为它也应该以一种简洁的方式工作,如果它工作,我会更新我的答案,而不是在我的座位上,所以现在无法检查@姆拉特布
$unionQuery = (new \yii\db\Query())
->from(['counter_items' => $archivedItemsQuery]);
$dataProvider = new \yii\data\ActiveDataProvider([
'query' => Products::find()->from(['products' => $archivedItemsQuery]),
'pagination' => [
'pageSize' => 10,
],
]);