使用具有相关模型的表单过滤yii2网格

时间:2016-08-25 14:54:52

标签: php yii2 yii2-advanced-app yii2-basic-app

使用带搜索表单的搜索模型,我想根据表单中输入的值过滤网格视图。表单字段是一个相关的表

enter image description here

我实际上是通过输入supplier_id来搜索tblpritems并在列pr_solicitation_id上过滤其网格,如tblprsuppliers

这是模型关系 在tblpritems

public function getPrSolicitation()
{
return $this->hasOne(Tblprsolicitations::className(), ['pr_solicitation_id'   
 => 'pr_solicitation_id']); 
}

在Tblprsolicitations模型中,通过

与tblprsuppliers相关
public function getPRsuppliers()
 {
  return $this->hasOne(Tblprsuppliers::className(), ['pr_solicitaion_id'
  => 'pr_solicitaion_id']);
}

我试过了

这是我的搜索表单(对于tblpritems)。这引用了tblpritems表中的pr_solicitation_id字段

 <?= $form->field($model, 'prSolicitation[pRsuppliers][supplier_id]')->textInput(['placeholder' => 'Enter supplier'])->label(false); ?>

但这不会过滤网格

这也是网格搜索

    public function search($params)
{
    $query = Tblpritems::find();

    // add conditions that should always apply here

    $dataProvider = new ActiveDataProvider([
        'query' => $query,
    ]);

    $this->load($params);


    // grid filtering conditions
    $query->andFilterWhere([
        'PRlineID' => $this->PRlineID,
        'Quantity' => $this->Quantity,
        'Unit_Price' => $this->Unit_Price,
        'Extended_price' => $this->Extended_price,
        'Currency_ID' => $this->Currency_ID,
        'PRID' => $this->PRID,
        'pr_solicitation_id' => $this->pr_solicitation_id,  //This is what am using t filter the grid
        'date_item_received' => $this->date_item_received,
        'Quantity_received' => $this->Quantity_received,
        'Received_by' => $this->Received_by,
        'item_received_status' => $this->item_received_status,
    ]);

    $query->andFilterWhere(['like', 'Tracking_Code', $this->Tracking_Code])
        ->andFilterWhere(['like', 'Description', $this->Description])
        ->andFilterWhere(['like', 'Remarks_on_receipt', $this->Remarks_on_receipt]);

    return $dataProvider;
}

为什么不起作用?

我也试过

<?= $form->field($model, 'prSolicitation->pRsuppliers[supplier_id]')->textInput(['placeholder' => 'Enter supplier'])->label(false); ?>   

但是这会返回一个只传递字符的错误

1 个答案:

答案 0 :(得分:0)

您需要在搜索模型中创建虚拟属性,在搜索规则中将其标记为安全,然后使用关系进行搜索,在where语句中添加过滤器,并在表单或网格中添加输入。

在搜索模型中添加虚拟属性:

public $supplier_id;

在您的规则中添加:

[['supplier_id'], 'safe'],

[['supplier_id'], 'integer'],

在搜索方法搜索中使用如下关系:

$query = Tblpritems::find();
$query->joinWith(['prSolicitation prSolicitation', 'prSolicitation.pRsuppliers pRsuppliers']);

在您的过滤器中添加:

$query->andFilterWhere([
    'PRlineID' => $this->PRlineID,
    'Quantity' => $this->Quantity,
    'Unit_Price' => $this->Unit_Price,
    'Extended_price' => $this->Extended_price,
    'Currency_ID' => $this->Currency_ID,
    'PRID' => $this->PRID,
    'pr_solicitation_id' => $this->pr_solicitation_id,  //This is what am using t filter the grid
    'date_item_received' => $this->date_item_received,
    'Quantity_received' => $this->Quantity_received,
    'Received_by' => $this->Received_by,
    'item_received_status' => $this->item_received_status,

    'prSolicitation.pRsuppliers.pr_supplier_id' => $this->supplier_id,

]);

表格:

<?= $form->field($model, 'supplier_id')->textInput(['placeholder' => 'Enter supplier'])->label(false); ?>

同时检查以下两个链接:

Yii 2.0: Displaying, Sorting and Filtering Model Relations on a GridView

Filter & Sort by calculated/related fields in GridView Yii 2.0