Search code examples
sessionyiishopping-cartcgridview

can we assign session values to cgridview without dataprovider


Mycart is stored in sessions which I wanna display in cgridview . I have used cgridview with dataprovider but can i do so with session value.Is it possible ?if not what would be the best way out to achieve so .If yes as I have no idea how do i proceed .

Please provide some guidance or examples.Do let me know if I'm not clear I'll make sure to clarify myself.


Solution

  • You could always create your own HTML table if you don't want to use data provider.

    Yii CGridView require a data provider, therefore you can't use CGridView if you can't supply the data provider. Use CArrayDataProvider instead:

    $sessionData = Yii::app()->user->getState('cart');
    // $dataProvider = new CArrayDataProvider($sessionData);   //throws undefined index:id exception
    $dataProvider = new CArrayDataProvider($sessionData, array('keyField'=>'id'));
    
    $this->widget('zii.widgets.grid.CGridView', array(
        'id' => 'your-grid',
        'dataProvider' => $dataProvider,
        'columns' => array(
            'item_name',
            'qty',
            // etc
        )
    ));