1
votes

I just learn and work with Yii2 and I was problem with filter in the LoanSearch model.

I have 3 tables, Customer, Person, and Loan.

for customerModel has relation:

public function getPerson()
{
    return $this->hasOne(Person::className(), ['id' => 'person_id']);
}

for LoanModel has relation:

public function getCustomer()
{
    return $this->hasOne(Customer::className(), ['id' => 'customer_id']);
}

Purpose: In the loan gridview, i want to filter with person name while the Customer table contain the foreign key of the Person.

Please help to guide me.

thanks.

1

1 Answers

0
votes

First you declare public property $person and $cutormer for searching and add Filters for that on your search model e.g:

->andFilterWhere(['like', 'person.name', $this->person])
->andFilterWhere(['like', 'customer.name', $this->customer]);

Also you should add safe rule for this properties e.g: [['person', 'customer'], 'safe'] on rules() method