Accessing Element Attribute In Dataprovider From Within A Foreach

Hi guys. As I’m new to Yii2 I’m stuck on a minor problem that I need help with. :)

Here is an action I added to my controller.

This controller finds all items in the model with the status "0"

What I’m trying to do is sum up total price on items in this particular model.

Can anyone wonderful Yii2 veteran point me in the right direction with this so I can stop scratching my hair off ? ;)

Best Regards


public function actionLokid()

    {

        $searchModel = new InnkaupSearch;


        // Returns all records that have the status "0" in stada(stada=icelandic for Status)

        $dataProvider = $searchModel->search(['InnkaupSearch'=>['stada'=>0]]);


        $totalPrice = 0;


        //Want to sum up total price on all items in dataProvider

        foreach($dataProvider as $record) {

            // How do I access attribute value of each object/record in model <img src='http://www.yiiframework.com/forum/public/style_emoticons/default/huh.gif' class='bbc_emoticon' alt='???' />

        }


        


        return $this->render('lokid', [

            'dataProvider' => $dataProvider,

            'searchModel' => $searchModel,


            // Total price on all items in dataProvider

            'total' => $total,

        ]);

    }

I recommend you read through this to understand how data providers work:

Quick answer:




$models = $dataProvider->getModels(); 



I’ll take a look at this, thanks Amnah :)

Any chance you could dumb it down a shade for me if I run into some more trouble ?