CGridView question

I have an actionCreate in a controller, which renders the create.php view:


$inshopItemsList=new inShopItem();  //creates an instance of a model to populate the CGridView.       

        

$this->render('create',array(

      'model'=>$model,

      'inshopItemsList'=>$inshopItemsList,

));

In the _form.php which is renderePartial in create.php view file I try to use CGridView the usual way:


<?php $this->widget('zii.widgets.grid.CGridView', array(

      'id'=>'user-grid',

      'dataProvider'=>$inshopItemsList->search(),

      'filter'=>$inshopItemsList

      ...

but it returns an empty set. Record is returned only when I change this:


'dataProvider'=>$inshopItemsList->search(),

'filter'=>$inshopItemsList

with this:


 'dataProvider'=>$inshopItemsList,

 //'filter'=>new inShopItem(),

and in a controller:


$inshopItemsList=new inShopItem(); 

to:


$inshopItemsList = new CActiveDataProvider(inShopItem::model()->active()->with('frontImage'), array(

'pagination' => array (

'pageSize' => Yii::app()->user->getState('pageSize', 20),

),

));

I want to use all the benefits of the built in filtering features but I don’t now how. Please, help.

Search methods looks like this and works correct for a CGridView in another view file:




	public function search()

	{

	// Warning: Please modify the following code to remove attributes that

	// should not be searched.


	$criteria=new CDbCriteria;


	$criteria->compare('itemID',$this->itemID,false);

	$criteria->compare('itemActive',$this->itemActive,true);

	$criteria->compare('itemBasePrice',$this->itemBasePrice,false);

	$criteria->compare('itemStockTotal',$this->itemStockTotal,false);


	$criteria->compare('itemCode',$this->itemCode,true);

	$criteria->compare('itemName',$this->itemName,true);

        $criteria->compare('itemBrandID',$this->itemBrandID,true);

        $criteria->compare('itemType',$this->itemType,true);

        $criteria->compare('itemSetSize',$this->itemSetSize,true);

        $criteria->compare('gender',$this->gender,true);

	$criteria->compare('itemBackName',$this->itemBackName,true);




	return new CActiveDataProvider(inShopItem::model()->active()->with('frontImage'), array(

		'criteria'=>$criteria,

		'pagination' => array (

		'pageSize' => Yii::app()->user->getState('pageSize', 20),

                 ),

            ));

	}



Are you talking about a create or view action?

In the latter case, your problem probably occurs because you omitted a call to unsetAttributes on the filter instance.

/Tommy

make the unsetAttributes call after -

$inshopItemsList=new inShopItem();


$inshopItemsList=new inShopItem('search');

$inshopItemsList->unsetAttributes();

actionCreate in controller


        

        ...


        $inshopItemsList=new inShopItem('search');

        if(isset($_GET['inShopItem']))

            $model->attributes=$_GET['inShopItem'];

        else

            $model->unsetAttributes();        

        

        $this->render('create',array(

            'model'=>$model,

            'inshopItemsList'=>$inshopItemsList,

        ));



create.php view file




...


        <?php $this->widget('zii.widgets.grid.CGridView', array(

            'id'=>'user-grid',

            'dataProvider'=>$inshopItemsList->search(),

            'filter'=>$inshopItemsList,


...



returns empty set. Why?

you have created a new variable $inshopItemsList but are using other variable, $model, in the next few lines.

replace $model with $inshopItemsList -


 if(isset($_GET['inShopItem']))

            $inshopItemsList->attributes=$_GET['inShopItem'];

        else

            $inshopItemsList->unsetAttributes(); 



The idea is the following.

In this actionCreate I create a record like this:





    public function actionCreate($id=null)

    {

        $model = new InshopSale();


        if(isset($_POST['InshopSale']))

        {

                

            if($model->validate()) {

                    if($model->save()) {

                    $this->redirect(array('inshopSale/index','justAdded'=>$model->id));

                }

            }

        }


        $inshopItemsList=new inShopItem('search');

        if(isset($_GET['inShopItem']))

            $model->attributes=$_GET['inShopItem'];

        else

            $model->unsetAttributes();        

        

        $this->render('create',array(

            'model'=>$model,

            'inshopItemsList'=>$inshopItemsList,

        ));

    }




but I need to populate CGridView in the create.php view file to make it possible for the user to choose items that would be added to the record that I am creating in a way of many to many relation.

can you share a screenshot of the create.php view file?

here it is:

1811

create.php

1812

_form.php

i meant screenshot of your screen (what you see in the browser).