K
K
ktotaika2018-11-08 15:17:39
Yii
ktotaika, 2018-11-08 15:17:39

How to organize search by related fields in Yii2?

Good afternoon!
I'm setting up a search in gridview, I ran into a banal problem. In the output table, fields from related tables are not searched. I tried many found examples and guides, did not help. Tell me, please, maybe I missed something out of sight, poke your nose.
Specialist model with User and Organization relationships:

class Specialist extends Specialists
{
    /**
     * @return \yii\db\ActiveQuery
     */
    public function getUser()
    {
        return $this->hasOne(UserModel::class, ['id' => 'id_user']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getOrganization()
    {
        return $this->hasOne(Organization::class, ['id' => 'id_organization']);
    }
}

Search Model
class SpecSearch extends Specialist
{
    public $userLogin;
    public $organizationShortName;

    /**
     * @return array
     */
    public function rules()
    {
        return [
            [['id'], 'integer'],
            [['fullname', 'organizationShortName', 'reg_date', 'userLogin'], 'safe'],
        ];
    }

    public function scenarios()
    {
        return Model::scenarios();
    }

    /**
     * @param $params
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $query = Specialist::find();

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

        $dataProvider->setSort(
        [
            'attributes' => ['id', 'fullname', 'userLogin']
        ]);

        if (!($this->load($params) && $this->validate())) {
            $query->joinWith(['organization', 'user'], true);
            return $dataProvider;
        }

        $query->andFilterWhere(
        [
            'id' => $this->id,
        ]);
        $query->andFilterWhere(['like', 'fullname', $this->fullname])
            ->andFilterWhere(['like', 'user.login', $this->userLogin])
            ->andFilterWhere(['like', 'organization.short_name', $this->organizationShortName])
            ->andFilterWhere(['like', 'reg_date', $this->reg_date]);

        return $dataProvider;
    }

}

Controller
public function actionIndex()
    {
        $searchModel = new SpecSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->get());

        return $this->render('index',
            [
                'dataProvider' => $dataProvider,
                'searchModel' => $searchModel,
            ]);
    }

View
<?= GridView::widget([
    'dataProvider' => $dataProvider,
    'filterModel' => $searchModel,
    'columns' =>

I would be grateful if you could advise.
Thanks

Answer the question

In order to leave comments, you need to log in

[[+comments_count]] answer(s)
D
Dmitry, 2018-11-08
@ktotaika

Good evening.
In the model

$query = Specialist::find()->with(['organization', 'user']);

$this->load($params);
if (!$this->validate()) {
     return $dataProvider;
}

In the controller
$searchModel = new SpecSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question