CHtml:Button can't pass object


I just started using yii. My problem is the following.

My controller, passes an object (lets call it myObject) to a view. I would like to have a CHtml:Button on this page, which initiates an action (lets say myAction).

echo CHtml::Button('PDF export',array('submit' => array('myAction','data'=>$myObject)));

The problem is, i would like to pass myObject to myAction. The error i get is,

urlencode() expects parameter 1 to be string, object given

So i can only pass a string with the button? Is there any way to pass the object to myAction?

is my question stupid? Should i solve this a completley different way? I am open to suggestions! Please help!

You should pass only the id, and in the action you can retrive the object from the database.

Each request is separate, so you cannot pass object trought request. With get/post var you can pass only string.


echo CHtml::Button('PDF export',array('submit' => array('myAction','id'=>$myObject->id)));

My object, i want to pass is a $dataProvider. My button supposed to export the result of the query to a PDF file. So the controller creates the query, passes the dataProvider to the, view. Here i have to create a button, wich generates the export. So it has to pass the $dataProvider, wich contains the results back to the controller!

Thus my object is not in the database, it is created by the controller.

Should i store the dataprovider in the database? I thougth about recreating the dataProvider again in the export action, but it is really a waste of resources, to do a query twice.

Thank you for your help!

IMO recreating the DataProvider is the way to go.

I know the feeling of "waste of resources" but as zaccaria pointed out: The Web is stateless by nature.

Question is: What is a greater waste of resources – Performing the query again -or- store and maintain the state (the query result) in the session (Yii::app()->user->setState/getState).

If you care about database queries storing the DataProvider won’t help because it does not contain the actual information but the query and settings to retrieve the specified pieces of information. You would need to always store the entire query result in the session and this would be a waste of resources, too. Why store the query result in the session just because some user might want to print it?


– David

Dmaus, you are probably right. I am quite new to web programing, and it is sometimes hard to forget the simple programing perspective.

I am going to repeat the query if someone choses to hit the export button.

Thanks for your advice!

There are really no options, you have to repeat the query.

You don’t have to pass the whole dataprovider, just sort and filters. Usually if you do the export in pdf you want to export all pages.

Be careful: if you have a great number of records, don’t use ActiveRecord but direct access the database, as it would be too expensive in term of memory.