Error With Data Provider To Cgridview

Hello guys hope you are well,

I’m a beginner in YII and is still learning it,

Ive tried to include a CGridView onto my view (which is the create form).

In my _form (view)




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

	'id'=>'principalcomments-grid',

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

	'filter'=>$model,

	'columns'=>array(

              array(

                'name'=>'staff_name',

                'value'=>'$data->timetable->academicstaff->nameWithInitials',

                ),

                'datecommented',

		'comment',

		array(

			'class'=>'CButtonColumn',

		),

	),

)); ?>



everything works fine but the filter is not working.

I modified my controllers’ actionCreate like:-




public function actionCreate()

	{

            

		$model=new Principalcomments;


		// Uncomment the following line if AJAX validation is needed

		// $this->performAjaxValidation($model);


                               

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

		{

			$model->attributes=$_POST['Principalcomments'];

			if($model->save())

                            $this->refresh();

			//	$this->redirect(array('view','id'=>$model->commentId));

		}

                

                $model=new Principalcomments('search');

		$model->unsetAttributes();  // clear any default values

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

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

                

                

                

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

			'model'=>$model,

		));

             

	}



Still this doesnt work, can someone please help me put it right…

Any help is greatly appreciated, thanks a lot…

Cheerz!

:)

I am a beginner too so this could probably be answered better by someone with more experience. Is CGridView used for a create view? I have used it with the index and admin views but I thought you use Active Form for those views.

Hi,

You need 2 instances of models.

One for the CGridView … it serves as a container of search parameters.

And the other for the creation form.




public function actionCreate()

{

	// model for the form

	$model_f=new Principalcomments;


	// Uncomment the following line if AJAX validation is needed

	// $this->performAjaxValidation($model_f);

                             

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

	{

		$model_f->attributes=$_POST['Principalcomments'];

		if($model_f->save())

                         $this->refresh();

	}


	// model for the grid

        $model_g=new Principalcomments('search');

	$model_g->unsetAttributes();  // clear any default values

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

		$model_g->attributes=$_GET['Principalcomments'];

                

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

		'model_f'=>$model_f,

		'model_g'=>$model_g,

	));

}



And use $model_f for the form and $model_g for the grid.

Hi, auren27

thanks for your reply,

Yes, i used it there.

As far as i understand CGridView can be used in any type of view (create, admin, etc).

Cheerz.! :)

Hi softark,

Thank you very much for the reply,

I changed the code as you said, but i got a "PHP notice - undefined variable model" on my create view.

I changed the code to render model_g and avoided the notice, but as far as i understand both model instances (model_g and model_f) will have be rendered. isnt that so?

So i tried :-

In my view (create.php)




echo $this->renderPartial('_form', array('model'=>$model_g)); 

echo $this->renderPartial('_form', array('model'=>$model_f)); 



but this gives me two views in the same form. is it possible to have both model instances to be rendered to one form?

Thanking you very much,

Cheerz! :)

‘$model_g’ and ‘$model_f’ are just the examples. You can use whatever names you like.

But the point is that you have to distinguish the 2 models in your view.




    $form = $this->beginWidget('CActiveForm', ...);

    echo $form->textBox($model_f, 'name', $options); // have to use the model for the form

    ...

    $this->widget('CGridView', array(

        'dataProvider' => $model_g->search(),   // have to use the model for the grid

        ...



Ah! got it, the distinguishing part was what i missed…

Thanks a lot for the help

Really appreciate it…

Cheerz! :)