Answer the question
In order to leave comments, you need to log in
How to filter GridView in Yii2 by related columns of the same table?
There is a table organizations with the following columns:
id - id of the organization
short_name - short name of the organization
full_name - full name of the organization
parent_organization - name of the parent organization.
The parent_organization field contains the organization id from the same table.
The attribute value displays the short name of the parent organization: .
The Organizations model has a getter:'value' => 'parentOrganization.short_name'
public function getParentOrganization()
{
return $this->hasOne(Organizations::className(), ['id' => 'parent_organization']);
}
select * from organizations_schema.organizations where parent_organization = ( select id from organizations_schema.organizations where short_name like '%Поисковый_запрос%' );
Answer the question
In order to leave comments, you need to log in
class OrganizationsSearch extends Organizations
{
public $parent_short_name;
/**
* @inheritdoc
*/
public function rules()
{
return [
[['parent_short_name'], 'string'],
// other rules
];
}
public function search($params)
{
$query = Organizations::find();
$query->alias('org');
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
$query->joinWith(['parentOrganization' => function($q) {
/**
* @var ActiveQuery $q
*/
$q->alias('par_org');
}]);
$this->load($params);
$dataProvider->setSort([
'attributes' => [
'parent_short_name' => [
'asc' => ['par_org.short_name' => SORT_ASC],
'desc' => ['par_org.short_name' => SORT_DESC],
],
// other attributes
],
'defaultOrder' => [
// defaultOrders
],
]);
if (!$this->validate()) {
return $dataProvider;
}
$query->andFilterWhere(['ilike', 'org.short_name', $this->short_name]);
$query->andFilterWhere(['ilike', 'par_org.short_name', $this->parent_short_name]);
return $dataProvider;
}
}
'attribute' => 'parent_short_name',
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question