[英]Yii2 - Sort and filter by one to many to one relation in GridView
I have 3 models: PurchaseOrders, PurchaseOrderItems, and Vendors. 我有3个模型:PurchaseOrders,PurchaseOrderItems和Vendors。 PurchaseOrders can have many PurchaseOrderItems tied to each one, and each PurchaseOrderItem can have one Vendor associated with it.
PurchaseOrders可以将许多PurchaseOrderItem绑定到每个,并且每个PurchaseOrderItem可以有一个与之关联的Vendor。 My dilemma, is that I need to not only show the Vendors in the PurchaseOrder GridView, but make that column capable of being filtered and sorted.
我的困境是,我不仅需要在PurchaseOrder GridView中显示供应商,还要使该列能够被过滤和排序。 I have figured out the MySQL query to get the Vendors associated with PurchaseOrder, but am at a loss in tying it all together with Yii2.
我已经找到了MySQL查询以使供应商与PurchaseOrder相关联,但是我不知道将它与Yii2捆绑在一起。
Query: 查询:
SELECT pos.id, pos.notes, group_concat(distinct(vend.name) order by vend.name ASC SEPARATOR ', ' ) as vendorNames
FROM purchase_orders as pos
JOIN purchase_order_items as PO_item
ON pos.id = PO_item.purchase_order_id
JOIN vendors as vend
ON PO_item.vendor_id = vend.id group by pos.id
PurchaseOrder Model: PurchaseOrder型号:
class PurchaseOrders extends \yii\db\ActiveRecord
{
public static function tableName()
{
return 'intra_purchase_orders';
}
public function getPurchaseOrderItems() {
return $this->hasMany(PurchaseOrderItems::className(['purchase_order_id' => 'id']);
}
}
PurchaseOrderItems Model: PurchaseOrderItems型号:
class PurchaseOrderItems extends \yii\db\ActiveRecord
{
public function getVendor()
{
return $this->hasOne(Vendors::className(), ['id' => 'vendor_id']);
}
}
Vendors Model: 供应商型号:
class Vendors extends \yii\db\ActiveRecord
{
public function getPurchaseOrderItems()
{
return $this->hasMany(PurchaseOrderItems::className(), ['vendor_id' => 'id']);
}
}
Is there a way to tie PurchaseOrders to Vendors, so that I can show Vendors associated with it like I'm doing in the MySQL query? 有没有办法将PurchaseOrders绑定到供应商,这样我就可以像在MySQL查询中那样显示与之关联的供应商?
Edit Forgot to add to the original question, I had created a function within the PurchaseOrder model to list the Vendors, however; 编辑忘记添加到原始问题,我在PurchaseOrder模型中创建了一个函数来列出供应商,但是; they are not sortable in the GridView this way.
它们不能以这种方式在GridView中排序。
public function getVendors() {
$vendor_arry = [];
foreach ($this->purchaseOrderItems as $key => $item) {
array_push($vendor_arry, $item->vendor->name);
}
sort($vendor_arry);
return implode(array_unique($vendor_arry, SORT_STRING), ", ");
}
With the below modifications, it should work fine. 通过以下修改,它应该工作正常。
PurchaseOrder model: PurchaseOrder型号:
<?php
class PurchaseOrders extends \yii\db\ActiveRecord
{
public $vendor_name; //This is the variable that will be used for filtering
public function rules()
{
return [
[['vendor_name'], 'string'] //Specify the variable as string
];
}
public static function tableName()
{
return 'intra_purchase_orders';
}
public function getPurchaseOrderItems() {
return $this->hasMany(PurchaseOrderItems::className(['purchase_order_id' => 'id']);
}
public function getVendors() {
$vendor_arry = [];
foreach ($this->purchaseOrderItems as $key => $item) {
array_push($vendor_arry, $item->vendor->name);
}
sort($vendor_arry);
return implode(array_unique($vendor_arry, SORT_STRING), ", ");
}
}
PurchaseOrderSearch model: PurchaseOrderSearch模型:
<?php
namespace {your_namespace};
use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use app\models\PurchaseOrder;
class PurchaseOrderSearch extends PurchaseOrder
{
public function rules()
{
return [
[['vendor_name'], 'safe'],
];
}
public function scenarios()
{
return Model::scenarios();
}
public function search($params)
{
$query = PurchaseOrder::find()->joinWith(['purchaseOrderItems.vendor']);
$dataProvider = new ActiveDataProvider([
'query' => $query
]);
$dataProvider->sort->attributes['vendor_name'] = [
'asc' => ['vendor.name' => SORT_ASC],
'desc' => ['vendor.name' => SORT_DESC],
];
$this->load($params);
if (!$this->validate()) {
return $dataProvider;
}
$query->andFilterWhere([
'id' => $this->id
]);
$query->andFilterWhere(['like', 'vendor.name', $this->vendor_name]);
return $dataProvider;
}
}
PurchaseOrder controller: PurchaseOrder控制器:
class PurchaseOrderController extends Controller
{
public function actionIndex()
{
$searchModel = new PurchaseOrderSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
}
And finally configure the grid view, 最后配置网格视图,
<?php
use yii\helpers\Html;
use app\extended\GridView;
?>
<?= GridView::widget([
'dataProvider' => $dataProvider,
'columns' => [
[
'header' => 'Vendors',
'attribute'=>'vendor_name',
'value' => function ($model, $key, $index) {
return $model->vendors;
},
]
],
]); ?>
<?= $this->render('_search', ['model'=>$searchModel]) ?>
Search view: _search.php 搜索视图:_search.php
<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
?>
<div class="search-form">
<?php $form = ActiveForm::begin([
'action' => ['index'],
'method' => 'get',
]); ?>
<?= $form->field($model, 'vendor_name') ?>
<div class="form-group actions">
<?= Html::submitButton('Search', ['class' => 'btn btn-primary']) ?>
</div>
<?php ActiveForm::end(); ?>
</div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.