Alias attribute causing Unknown Property Exception

I used MySQL function in select to create a qty_available column, then I added that attribute in the rules under safe and created a public variable for qty_available, but I still kept getting Unknown Property Exception. Why does this happens?

My Search Model

    public $qty_available;


    public function rules()


        return [

            [['product', 'qty_available'], 'safe'],



public function search($params)


        $query = Product::find()->joinWith('stocktrxns', true, 'LEFT JOIN')

                ->select(['product', 'qty_available' => '(SUM(CASE WHEN stock.is_out = false THEN qty ELSE 0 END) -

        SUM(CASE WHEN stock.is_out = true THEN qty ELSE 0 END))'])

                ->leftJoin('stock', 'stock.tra_no = stocktrxn.tra_no')


GridView doesn’t recognize it, throws Unknown Property Error:



                    'dataProvider' => $inventory,

                    'filterModel' => $inventorySearch,

                    'columns' => [






You have to declare $qty_available in Product model, not in ProductSearch.

You’re right, I moved the attribute to the model instead of the search model, it now works! Thanks!