php - How do i filter model instances respect to its virtual attribute in yii2 model using ActiveDataProvider?

one text

I have Product class and it contain two attribute 'price' and 'sale_price' and also i have a getter getOriginalPrice.

/**
 * This is the base model class for table "product".
 *
 * more attribute here..
 * @property float $price
 * @property float|null $sale_price
 * more attribute here...
 */
class Product extends \yii\db\ActiveRecord
{
    /**
     * get product original price 
     * 
     * @return float
     */
    public function getOriginalPrice()
    {
        if(!is_null($this->sale_price) && (float)$this->sale_price > 0)
        {
            return $this->sale_price;
        }

        return $this->price; 
    }

}

so my question is that how can i filter the Product model respect to this virtual attribute originalprice?

i have tried to filter the model using product/index?sort=originalprice and product/index?sort=-originalprice in the url and put this code in ProductSearch model but no luck.

$query = Product::find();

        $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;
        }

      
        //filtering conditions
        $query->andFilterWhere([
            'price' => $this->price,
            'originalprice' => $this->originalprice,
            'product_cat_id' => $this->product_cat_id,
        ]);


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

        return $dataProvider;

does anybody knows how to solve it?

Source