[SOLVED] Save cGridView search results

Hi All,

Back to cGridView

I abandoned ajaxed version of cGridView for the one that uses PHP calls (‘ajaxUpdate’=>false) because I’m not guru at yii-ajax, and constant fight how to get simple things was consuming my time.

Let me shortly describe what I want to achieve… I have standard but non-ajaxed cGridView on which I use filtering/searching. Then I would like to save the results to database to new table.

Let’s say when I open the grid (standard manage model " link") it loads all rows from model:

C D E

[color="#000080"]1 AAA XYZ

2 AAB YHD

3 BBF HUO

4 NNM FSS[/color]

Now I filter on column "D" using "AA" so after PHP call to server I get:

C D E

[color="#000080"]1 AAA XYZ

2 AAB YHD[/color]

Now I want to save these search results for future usage. My idea is to save keys in format "1,2" so later the same grid can be opened with condition where id IN (1,2) - along comma separated keys I want to save user id, which performed the search, time, name of this result…

My view admin.php looks like:


<?php 

$dataProvider=$model->search();

$currentItems=$dataProvider->getItemCount();


echo CHtml::form();


//this gives me ID of currently returned rows

$data=$dataProvider->getKeys();


//this saves the returned IDs in format "1,2".... (adding comma after IDs except last ID)

for($i=0;$i<count($data);$i++)

{

if($i<count($data)-1)

{

$searchlist_items.=$data[$i].',';

}

else

{

$searchlist_items.=$data[$i];

}

}


//I store these values in hidden fields

echo '<input id="business_total_items" type="hidden" value="'.$this->totalItems().'" >';

echo '<input id="business_current_items" type="hidden" value="'.$currentItems.'" >';


//////////////////////////////////////////////// var $searchlist_items is counted in above "for" loop

echo '<input id="business_searchlist_items" type="hidden" value="'.$searchlist_items.'" >';

//////////////////////////////////////////////// var $searchlist_items is counted in above "for" loop


echo '<input id="business_current_user" type="hidden" value="'.Yii::app()->user->id.'" >';

echo '<input id="business_searchlist_now" type="hidden" value="'.date("Y-m-d H:i:s").'" >';

?>

Only $this->totalItems() call method from controller:


public function totalItems()

{

return Business::model()->count();

}

Rest is done in the view.

My problem is that I think I’m getting dirty with putting some logic here like this “for loop”. But I cannot put it to controller because calling $dataProvider there as model->search() always returns all rows from model. Calling it in the view returns currently returned rows.

Do you think this "for loop" breaks MVC approach? Maybe you have some ideas how and want to put in the controller?

And how can I save values from hidden inputs to new database table? - completely have no idea here…

Thanks in advance

Tom

I think that you can achive this saving in the controller, withouht touching the view.

In the controller, when you set the attributes to $model you can save this array in Yii::app()->user->setValue(‘searchGridA’, $_POST[‘modelID’]).

Something like:




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

{

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

    Yii::app()->user->setValue('searchGridA', $_POST['modelID']);

}

else

{

   $model->attributes=Yii::app()->user->getValue('searchGridA');

}



The cGridView displays items from [color="#000080"]MODEL1[/color].

[b]C D E

1 AAA XYZ

2 AAB YHD

3 BBF HUO

4 NNM FSS [/b]

After I perform search/filter od D column with "AA" strig and I get for example this:

[b]C D E

1 AAA XYZ

2 AAB YHD[/b]

Now I want to save this result in [color="#006400"]MODEL2[/color]. As search result I want to save "1,2" (IDs of result rows). Except that to MODEL2 I want to save current user, time etc…

So data in MODEL2 will look like:


ID NAME ITEMS_SAVED USER  TIME

1  XYZ  1,2         admin 2010-11-17 16:12:23

How can I save to [color="#006400"]MODEL2[/color] data that comes in $_POST array from view/model1?

Model1Controller.php:

public function actionSaveToModel2()


{


	&#036;model=new Model2;





	if(isset(&#036;_POST['Model1']))


	{


		&#036;model-&gt;attributes=&#036;_POST['Model1'];


		if(&#036;model-&gt;save())


			&#036;this-&gt;redirect(.............);


	}


}

What if $_POST[‘Model1’] attribute names are dirrefent from MODEL2 attributes?

What if MODEL2 has more properties/fields that $_POST[‘Model1’]? Can I add in controller extra fields?

Yes, you can set additional properties in the controller explicitly. You don’t need to use the attributes assignment method unless it does what you want it to do. In fact, if you’re trying to assign a model that is very different (not extended from the other) then you should probably not use the attribute assignment to the second model class.

Pull in just the values that you want from the posted object:




public function actionSaveToModel2()

{

   $model=new Model2;


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

   {

      // be careful here you're not overriding the ID if it's set within the rules

      //$model->attributes=$_POST['Model1'];


      $postedModel = Model1::model()->findByPk( $_POST['Model1']['id'] );

      // Optional - depends on if you're actually letting them alter the Model1 data

      $postedModel->attributes = $_POST['Model1']; 


      // Explicitly set the values from Model1 that you want to attach to Model2

      // Possibly sufficient just to save the ID and then reference it later via relation?

      $model->attribute_1 = $postedModel->attribute_1; 


      // Set additional information specific to the new model -- whatever you need

      $model->owner_id = Yii::app()->user->id;

      $model->dtCreated = new CDbExpression('NOW()');


      if($model->save())

         $this->redirect(.............);

   }

}



Is that what you’re trying to do?

Dana

Thanks for your input.

Yes, MODEL2 is not extended from MODEL1 in any way, so I think I abandon attribute assignment.

From the $_POST array of MODEL1 I can pull all values/attributes then I add some custom values as MODEL2 attributes. To be honest I was breaking MVC pattern by defining user_id and time_now() in the view, as the hidden inputs. Then I submitted it to controller. And this is to be done out in controller as you pointed:




$model->owner_id = Yii::app()->user->id;

$model->dtCreated = new CDbExpression('NOW()');



Yes you guessed my ideas right.

Thanks

Thanks Dana for your input. I did this that way:

In the Model1Controller.php:




public function savePostedDataToModel2()

{

$model = new Model2;

$model->attributte1 = some_explicit_value1; 	

$model->attributte2 = $_POST['some_value_from_submitted_form1']; 

...

// MORE VALUES/ATTRIBUTTES

...

$model->date_created = date("Y-m-d H:i:s"); 	

	

if($model->save())

{

	echo 'New Model was successfully saved';

}

else

{

	echo 'Model was NOT saved';

}

	

}



And it works like a charm. What’s more I use Yii’s [color="#FF00FF"]c[/color][color="#006400"]o[/color][color="#000080"]o[/color][color="#A0522D"]l[/color] CHtml::ajaxSubmitButton and I get result output underneath it :wink: