Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
241 views
in Technique[技术] by (71.8m points)

php - Filtering yii2 grid using a form with related model

Am using a search model with a search form and i would like to filter a grid view based on the value entered in the form. The form field is of a related table

enter image description here

Am actually searching tblpritems and filtering its grid on the column pr_solicitation_id by entering supplier_id as in the tblprsuppliers

This is the model relationships In the tblpritems

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

In the Tblprsolicitations model is related to tblprsuppliers by

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

I have tried

This is my search form (for the tblpritems). This references the pr_solicitation_id field in the tblpritems table

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

But this does not filter the grid

This is also the grid search

    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;
}

Why is it not working?

I have also tried

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

but this returns an error of only characters should be passed

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You need to create a virtual attribute in your search model, mark it as safe in your search rules, then search with relations, add the filters in where statement and add the input in the form or grid.

In your search model add a virtual attribute:

public $supplier_id;

In your rules add:

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

or

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

In search method search with relations like this:

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

And in your filters add:

$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,

]);

In form:

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

Also check this two links:

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

and

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...