Passing params to an action

I’m trying to insert a record in the background using an action. For instance, I am looking at a content type and want to insert a favorite for it. I am trying to pass enough information from the current content record to an action method in the UserFavoriteController. I can’t get the structure right for the multiple params.

I need to pass $title, $description, $what (what content table), $item (the id of the content item).

Here’s the code in the content view file:




<?php

		$title=$model->formatted_name;

		$description=$model->description;

		$what='contact';

		$item=$model->id;

		$details=array('title'=>$title, 'description'=>$description, 'what'=>$what, 'item'=>$item);

		$where='/hawkwebapp/index.php?r=userfavorite/add&details='.$details; 

?>


<a href=<?php echo $where;?> >

	<image src="/hawkwebapp/assets/hawk/icons/googlePlus/color18/star.png" alt="Add Favorite" align="right" />

    </a>




Here’s the code from the action method in the UserFavoriteController:




public function actionAdd($details)

	{

		$title=$details->title;

		$description=$details->description;

		$what=$details->what;

		$item=$details->item;

		

		$favorite=new UserFavorite;

		$favorite->user_id=Yii::app()->user->id;

		$favorite->title=$title;

		$favorite->description=$description;

		$favorite->what=$what;

		$favorite->where='mainDB';

		$favorite->item_id=$item;

		$favorite->behavior='';

		$favorite->weight=1;

		$favorite->status='Active';

		$favorite->insert_datetime=time();

		$favorite->last_edit_datetime=time();

		$favorite->insert_datetime_utc=$favorite->insert_datetime;

		$favorite->last_edit_datetime_utc=$favorite->last_edit_datetime;

		$favorite->save();

		

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

			'model'=>$favorite,

		));

		

		$this->redirect(Yii::app()->user->returnURL);

	}



The php error is: Trying to get property of non-object

This happens at the first line accessing a param:

$title=$details->title;

Everything is new to me now, so I can’t see where the problem is.

Can you?

Thanks,

Alex

Hi Alex,

You can’t just pass arrays as parameters, they’re a bit more delicate.

Take a look at the url’s you are generating, I’m guessing they will look a lot like /hawkwebapp/index.php?r=userfavorite/add&details=Array

What PHP is telling you with that error-message is that the parameter your action receives is the string “Array” which it is not able to derive any more information such as title from. You’ll need to pass all the elements of $details as separate URL parameters. I’d also highly recommend using the createUrl-method instead of manually concatenating url-pieces.

Excellent. Thanks so much. After just a bit of further research and finagling I got it to work perfectly.

Here’s the result in the view file:




<?php

		$title=$model->formatted_name;

		$description=$model->description;

		$what='contact';

		$item=$model->id;

		$url=Yii::app()->createUrl('userfavorite/add', array('title'=>$title, 'description'=>$description, 'what'=>$what, 'item'=>$item));

	?>

<a href=<?php echo $url;?> >

	<image src="/hawkwebapp/assets/hawk/icons/googlePlus/color18/star.png" alt="Add Favorite" align="right" />

    </a>



And here’s the result in the controller:




public function actionAdd($title,$description,$what,$item)

	{	

		$favorite=new UserFavorite;

		$favorite->user_id=Yii::app()->user->id;

		$favorite->title=$title;

		$favorite->description=$description;

		$favorite->what=$what;

		$favorite->where='mainDB';

		$favorite->item_id=$item;

		$favorite->behavior='';

		$favorite->weight=1;

		$favorite->status='Active';

		$favorite->insert_datetime=time();

		$favorite->last_edit_datetime=time();

		$favorite->insert_datetime_utc=$favorite->insert_datetime;

		$favorite->last_edit_datetime_utc=$favorite->last_edit_datetime;

		$favorite->save();

		

		$this->redirect(Yii::app()->user->returnURL);

	}



Best,

Alex

This is very helpful. Thanks!

Does anyone have a good example of how to POST the same kind of data? Just a simple form POST routine I guess?