How to organize a search on a linked field?

Do a search on a linked field. accordingly, in order found a category have sub-categories, in the field it is necessary to introduce id_category and not the category name. how to fix it?

model
class Subcategory extends SubcategorySearch
{
/**
 * {@inheritdoc}
*/
 public function rules()
{
 return [
 [['id'], 'integer'],
 [['name', 'category_id'], 'safe'],
 [['name'], 'string', 'max' => 100],
];
}

/**
 * {@inheritdoc}
*/
 public function scenarios()
{
 // bypass scenarios() implementation in the parent class
 return Model::scenarios();
}

/**
 * Creates data provider instance with search query applied
*
 * @param array $params
*
 * @return ActiveDataProvider
*/
 public function search($params)
{
 $query = Subcategory::find();

 // add conditions that should always apply here

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

$this->load($params);

 if (!$this->validate()) {
 // uncomment the following line if you do not want to return any records when validation fails
//$query->where('0=1');
 return $dataProvider;
}

 // grid filtering conditions
$query->orFilterWhere([
 'id' => $this->id,
 //'category_id' => $this->category_id,
 ])->orFilterWhere(['like', 'name', $this->name])->orFilterWhere(['like', 'category_id', $this->category_id]);

 return $dataProvider;
}

}

view
<?php $form = ActiveForm::begin([
 'action' => ['index-subcategory'],
 'method' => 'get'
 ]); ?>


 <?= $form->field($model, 'id') ?>

 <?= $form->field($model, 'category_id') ?>

 <?= $form->field($model, 'name') ?>

 <div class="form-group">
 <?= Html::submitButton('Search', ['class' => 'btn btn-primary']) ?>
 <?= Html::resetButton('Reset', ['class' => 'btn btn-outline-secondary']) ?>
</div>

 <?php ActiveForm::end(); ?>


Controller
public function actionIndexSubcategory()
{
 $searchModel = new SubcategorySearch();
 $dataProvider = $searchModel->search(\Yii::$app->request->queryParams);

 return $this->render('index-subcategory', [
 'searchModel' => $searchModel,
 'dataProvider' => $dataProvider,
]);
 }
March 23rd 20 at 18:41
1 answer
March 23rd 20 at 18:43
Solution
thank you! - Rudy.Roob81 commented on March 23rd 20 at 18:46
can you tell me where is the error? can't figure out... writes this "SQLSTATE[42S22]: Column not found: 1054 Unknown column 'category.name' in 'where clause'
The SQL being executed was: SELECT COUNT(*) FROM `subcategory` WHERE (`name` LIKE '%some%') OR (`category`.`name` LIKE '%assessment%')"
<?php
class Subcategory extends SubcategorySearch
{
 public $category_id;
/**
 * {@inheritdoc}
*/
 public function rules()
{
 return [
 [['id'], 'integer'],
 [['name', 'category_id'], 'safe'],
 [['name'], 'string', 'max' => 100],
];
}

/**
 * {@inheritdoc}
*/
 public function scenarios()
{
 // bypass scenarios() implementation in the parent class
 return Model::scenarios();
}

/**
 * Creates data provider instance with search query applied
*
 * @param array $params
*
 * @return ActiveDataProvider
*/
 public function search($params)
{
 $query = Subcategory::find();

 // add conditions that should always apply here

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

 $dataProvider->sort->attributes['category_id'] = [
 // The tables are the ones our relation are configured to
 // in my case they are prefixed with "tbl_"
 'asc' => ['category.name' => SORT_ASC],
 'desc' => ['category.name' => SORT_DESC],
];

$this->load($params);

 if (!$this->validate()) {
 // uncomment the following line if you do not want to return any records when validation fails
//$query->where('0=1');
 return $dataProvider;
}

 // grid filtering conditions
$query->orFilterWhere([
 'id' => $this->id,
 //'category_id' => $this->category_id,
 ])->orFilterWhere(['like', 'name', $this->name])->orFilterWhere(['like', 'category.name', $this->category_id]);

 return $dataProvider;
}


}
- Rudy.Roob81 commented on March 23rd 20 at 18:49
@Rudy.Roob81, there the example is.

$query->joinWith([<relative_model_name>]); - Willa.Watsica71 commented on March 23rd 20 at 18:52
@Willa.Watsica71, thank you, overlooked - Rudy.Roob81 commented on March 23rd 20 at 18:55
@Rudy.Roob81, please - Willa.Watsica71 commented on March 23rd 20 at 18:58

Find more questions by tags Yii