[Solved] Cgridview Action = Error 400 (See 1St Reply )

hi everybody ! :)

I want to have a grid which display all data from the table "Personne" which have the ID of the event in to FK column.

So what I did. In my controller I get the list of person and I sent it to my view.




$listPersonne = Personne::searchByEvent($idEvent);


$this->render("viewInscriptions",array(

	'listPersonne'=>$listPersonne,

	'idEvent'=>$idEvent,

));



it works but all data are in an array.

so in my view, I transform the data to have a dataprovider for my CGridView.




$dataProvider =  new CArrayDataProvider('Personne');

$dataProvider->setData($listPersonne);



and then I try to list the object :




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

	//'id'=>'personne-grid',

	'dataProvider'=>$dataProvider,

	//'filter'=>$listPersonne,

	'columns'=>array(				

		'Id_Personne',

		

		

		array(

			'class'=>'CButtonColumn',

		),

	),

));



an I have this error :


Php notice 

- Trying to get property of non-object 

D:\ ... \yii\framework\base\CComponent.php(612) : eval()'d code(1)

So I wonder if I take the good way to do this?

and if yes, how to solve the error?

More information :

Var_dump on dataprovider :




object(CArrayDataProvider)#23 (11) { 

["keyField"]=> string(2) "id" 

["rawData"]=> string(<img src='http://www.yiiframework.com/forum/public/style_emoticons/default/cool.gif' class='bbc_emoticon' alt='8)' /> "Personne" 

["caseSensitiveSort"]=> bool(true) 

["_id":"CDataProvider":private]=> NULL 

["_data":"CDataProvider":private]=> array(1) { 

	[0]=> array(32) { 

		["Id_Personne"]=> string(1) "1" 

		["DateInscription"]=> string(10) "2014-03-10" 

		["Statut"]=> string(10) "pr�inscrit" 

		["Politesse"]=> string(1) "0" 

		["Nom"]=> string(1) "0" 

		["Prenom"]=> string(1) "0" 

		["Adr_Numero"]=> string(1) "0" 

		["Adr_Boite"]=> NULL 

		["Adr_Rue"]=> string(1) "0" 

		["Adr_CodePostal"]=> string(1) "0" 

		["Adr_Localite"]=> string(1) "0" 

		["Adr_Pays"]=> string(1) "0" 

		["Email"]=> string(1) "0" 

		["TelFixe"]=> NULL ["Gsm"]=> NULL 

		["TelFax"]=> NULL 

		["Organisation"]=> string(1) "0" 

		["Fonction"]=> string(1) "0" 

		["AdrFacture"]=> NULL 

		["NumTVA"]=> NULL 

		["CommStructuree"]=> string(1) "0" 

		["AttestationPresence"]=> NULL 

		["Recu"]=> NULL ["Facture"]=> NULL 

		["Act_Statut"]=> NULL 

		["Act_Prix"]=> NULL 

		["Dvd_Statut"]=> NULL 

		["Dvd_Prix"]=> NULL

		["Remarque"]=> NULL 

		["PrixTot"]=> string(4) "0.00" 

		["Id_Evenements"]=> string(1) "1" 

		["Id_Qualite"]=> string(1) "1" 

	} 

} 

["_keys":"CDataProvider":private]=> NULL 

["_totalItemCount":"CDataProvider":private]=> NULL 

["_sort":"CDataProvider":private]=> NULL 

["_pagination":"CDataProvider":private]=> NULL 

["_e":"CComponent":private]=> NULL 

["_m":"CComponent":private]=> NULL




SOLUTION : Add the id column name of the object to the keyfield . (not ‘id’ by default).

$dataProvider = new CArrayDataProvider(‘Personne’, array(‘keyField’=>‘Id_Personne’,

														'data'=&gt;&#036;listPersonne,


														'pagination'=&gt;array('pageSize'=&gt;'20')));

But I still can’t add

	array(


		'class'=&gt;'CButtonColumn',


	),

In the columns to have button.

Actually, I was totally wrong.

I don’t have to take this way with yii… (Or I just don’t use it … )

So I made something different, and its works too but I have an other problem.

I read these articles to be more familia with CGridView and other data viewer :

they are really interesting.

and now I have a CGridView which show the correct data but …

I have an error 400 when I try to search, to switch page, to sort or any action on the CGrid : "NetworkError: 400 Bad Request"

(this when I try to switch to page 2 : "NetworkError: 400 Bad Request - http://localhost/LayoutColloques/index.php/admin/searchPersonne?Id_Evenements=45&ajax=yw1&page=2" )

(this when I try to sort on the Id_Personne :

GET http://localhost/LayoutColloques/index.php/admin...rsonne?Id_Evenements=1&ajax=yw1&sort=Id_Personne

400 Bad Request "NetworkError: 400 Bad Request - http://localhost/LayoutColloques/index.php/admin/searchPersonne?Id_Evenements=1&ajax=yw1&sort=Id_Personne"

And I don’t know why. this is the step.

My action calling the event view :




	public function actionView($id)

	{

		$personne = new Personne('search');

		$personne->unsetAttributes();

		

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

			$personne->attributes = $_GET['personne'];

			

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

				'evenement'=>EvenementController::loadModel($id),'personne'=>$personne

		));

	}



my first view : (showing an event)




- bla bla on the page

- CDerailView showing the event

$this->renderPartial('_searchPersonnesByEvent' , array('personne'=>$personne, 'evenement'=>$evenement));

// to show persons registered on this event



My CGridView in a renderpartial :




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

		'ajaxUrl' => $this->createUrl('searchPersonne', array('IdEvent' => $evenement->Id_Evenements)), 

		'dataProvider' => $personne->searchByEvent($evenement->Id_Evenements, 'searchPersonne', array('Id_Evenements' => $evenement->Id_Evenements)), 

		'filter' => $personne,


		'columns'=>array(

				array('name' => 'Id_Personne',  	'header'=>'Id Personne'),

				array('name' => 'DateInscription', 	'header'=>'Date Inbscription'),

			),

));?>

my searchPerson Action in my controller :




	//this action can be used by ajax (directly from CGridview)

	public function actionSearchPersonne($idEvent) {

	

		$personne = new Personne('search');

		$personne->unsetAttributes();

	

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

			$personne->attributes = $_GET['personne'];

	

		$this->renderPartial('_searchPersonnesByEvent', array('personne' => $personne));

	}



and finnally my Personne model, and more specificly the function used to show personne data :


public function searchByEvent($idEvent, $routeUrl, $templateParams) {

	

		$criteria = new CDbCriteria;

	

		//$criteria->compare('id', $this->id);

		$criteria->compare('Id_Evenements', $idEvent);

		$criteria->compare('Id_Personne', $this->Id_Personne);

		

	

		$item_count = self::model()->count($criteria);

	

		$pages = new CPagination($item_count);

		$pages->setPageSize(10);

	

		$sort = new CSort();

	

		if (!empty($templateParams)) {

			$pages->params = $templateParams;

			$sort->params = $templateParams;

		}

	

		if ($routeUrl) {

			$pages->route = $routeUrl;

			$sort->route = $routeUrl;

		}

	

		return new CActiveDataProvider($this, array(

				'criteria' => $criteria,

				'pagination' => $pages,

				'sort' => $sort,

		));

	}

and soi the problem is that I can’t switch page if I have a lot of result, I can’t search by entering data at the top of the column, or sort a column without having an error 400 with not so much of details …

do you have an Idea?

I hope to be clear ^^ :)

have a nice day.

Hi Vincent,

I think that actionSearchPersonne could be much simpler:




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

    'ajaxUrl' => $this->createUrl('searchPersonne', array('IdEvent' => $evenement->Id_Evenements)), 

    'dataProvider' => $personne->searchByEvent($evenement->Id_Evenements), 

    'filter' => $personne,

    'columns'=>array(

        array(

            'name' => 'Id_Personne',

            'header'=>'Id Personne'

        ),

        array(

            'name' => 'DateInscription',

            'header'=>'Date Inbscription'

        ),

    ),

));?>


...


public function searchByEvent($idEvent) {

	$criteria = new CDbCriteria;

	//$criteria->compare('id', $this->id);

	$criteria->compare('Id_Evenements', $idEvent);

	$criteria->compare('Id_Personne', $this->Id_Personne);

	

	return new CActiveDataProvider($this, array(

		'criteria' => $criteria,

		'pagination' => array(

			'pageSize' => 10,

		),

	));

}



Thanks for your answer ! :)

You are totally right. now, I can sort data and switch pages.

but still can’t search for a specific record with the filter line. I’m looking for why. if you have any suggestion? :)

edit 1 : I don’t have error 400 when I comment the ajax line in the definition of the CGridView but nothing is done.

What is the purpose exactly of this line? :s

edit 2 : there were some syntax errors : IdEvent at the place of idEvent. and I didn’t pass the ‘evenement’ in my searchPersonne action.


$this->renderPartial('_searchPersonnesByEvent', array('personne' => $personne,  'evenement'=>Evenement::model()->findByPk($idEvent)));

and also $_GET[‘personne’] instead of $_GET[‘Personne’] in my controller.

Now, I think my filter works. But, because there is always a “but”, the render partial in my actionSearchPersonne of my controller didn’t show anything.

In fireBug I see the get request :

GET : http://localhost/LayoutColloques/index.php/admin/searchPersonne?Personne[Id_Personne]=38...all other personne attribute …idEvent=45

and if I copy past this url in my browser, I have the right CGridView with my filtered result in a new page.

So I think he just didn’t appear with the renderpartial of my controller.

I’m keeping looking for why and I’m still aware for any suggestions :)

edit 3 : all works fine but didn’t appear :

My action used by the ajax event.




public function actionSearchPersonne($idEvent) {

	

		$personne = new Personne('search');

		$personne->unsetAttributes();

		

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

		{			

			$personne->attributes = $_GET['Personne'];			

		}

		

		$this->renderPartial('_searchPersonnesByEvent', array('personne' => $personne,  'evenement'=>Evenement::model()->findByPk($idEvent)));

	}

when I use $this->renderpartial Sort and switch Pages works fine.

when I use $this->render my filtering works (one time, then I have to reload the page to have all my data again) and when I try to sort or switch page, my cgridview disappear …

just Why ! ?

Hi,

discovered something here for your filtering problem:


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

				'evenement'=>EvenementController::loadModel($id),'personne'=>$personne

		));

You need your class object, get it with $this:


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

				'evenement'=>$this->loadModel($id),'personne'=>$personne

		));

Thanks but I can’t because i’m not in my EvenementController (I’m in my adminController) but now if I want to load a model I use


Evenement::model()->findByPk($idEvent)

I think it’s more appropriate.

Thanks for reading me and trying to help. :)

By the way, now all actions works fine but aren’t displayed :(

see the edit of my previous post… and thanks in advance ! :)

Have a good day.

edit :

Now it works… I hope ^^ thx to all again :)

I’ll show how I did if someone have same trouble.

what I did : I delete my partialView and put the CGrid on the same view as the event. (where the partial view was called)

and then I have my event view, and on this my CGridView with only the person register on this event :


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

		'ajaxUrl' => $this->createUrl('searchPersonne', array('idEvent' => $evenement->Id_Evenements)), // this takes care of the search using searchPersonne action and event id

		'ajaxUpdate' => 'personneList',

		

		//'dataProvider' => $personne->searchByEvent($evenement->Id_Evenements, 'searchPersonne', array('Id_Evenements' => $evenement->Id_Evenements)), //filter by event, routing to searchPersonne action, extra parameter of event id generated to the next request (like sort column links e.t.c)

		'dataProvider' => $personne->searchByEvent($evenement->Id_Evenements),

		

		'filter' => $personne,


		'columns'=>array(

				array('name' => 'Id_Personne','header'=>'Id Personne'),

                                 ...

				),

			),

));?>

<div id="personneList">

</div>



the action in my controller used by ajax which render the eventView again and not the partial view




	//this action can be used by ajax (directly from CGridview)

	public function actionSearchPersonne($idEvent) {

	

		$personne = new Personne('search');

		$personne->unsetAttributes();

		

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

		{			

			$personne->attributes = $_GET['Personne'];			

		}

		$this->render('viewEvent',array('evenement'=>Evenement::model()->findByPk($idEvent),'personne'=>$personne));

		//$this->render('_searchPersonnesByEvent', array('personne' => $personne,  'evenement'=>Evenement::model()->findByPk($idEvent)));

	}



and my searchByEvent method in my Personne model




public function searchByEvent($idEvent) {

		

		$criteria = new CDbCriteria;


		$criteria->compare('Id_Evenements', $idEvent);

		$criteria->compare('Id_Personne', $this->Id_Personne);

		... all criteria ....

		

	

		return new CActiveDataProvider($this, array(

				'criteria' => $criteria,

				'pagination' => array(

						'pageSize' => 10,

				),

		));

	}



and I think it’s all.

thanks again to everybody which help me and other who tried :)

have a good day.