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