GridView Filtering on a Relational Column

I am trying to filter on a relational data column.

In my search model, I added the field


public function attributes()

	{        

		// add related fields to searchable attributes

		return array_merge(parent::attributes(), ['customerProductBaseProduct.product_name');

	}

Made it a safe search field


[['customerProductBaseProduct.product_name'], 'safe'],

To the search function, I added a $query->joinWith


$query = CustomerProducts::find();

		

        // add conditions that should always apply here

		$query->joinWith(['customerProductBaseProduct']);

		

        $dataProvider = new ActiveDataProvider([

            'query' => $query,

        ]);

		

        $this->load($params);

And a ->andFilterWhere


$query->andFilterWhere(['like', 'customer_product_customerID', $this->customer_product_customerID])

            ->andFilterWhere(['like', 'customer_product_formula', $this->customer_product_formula])

            ->andFilterWhere(['like', 'customer_product_name', $this->customer_product_name])

            ->andFilterWhere(['like', 'customer_product_sub_name', $this->customer_product_sub_name])

            ->andFilterWhere(['like', 'customer_product_spanish', $this->customer_product_spanish])

            ->andFilterWhere(['like', 'customer_product_category', $this->customer_product_category])

            ->andFilterWhere(['like', 'customerProductBaseProduct.product_name', $this->customerProductBaseProduct]);

The column does nothing when I try to filter. What am I doing wrong?

If you are using table prefix (often ‘tbl_’), then you must include it so you would have:


$query->andFilterWhere(['like', 'tbl_customerProductBaseProduct.product_name', $this->customerProductBaseProduct]);

But in any case, what I usually do is I display the resulting query to verify:




var_dump($query->prepare(Yii::$app->db->queryBuilder)->createCommand()->rawSql);

exit;