How to use a DataProvider
#1
Posted 02 June 2012 - 06:29 AM
All I want to do is step through the results of a search in a dataprovider, and make an array of the id attributes. This should be easy, but it appears to be arcane.
Say I have a model Balls. I want to search for all the red Balls, display the records in a CGridView, let the user select some of the records, then return the selected records and an array of ALL the ids to the BallsController. Should be easy, right? But I cannot for the life of me figure out how to step through the records in the dataprovider. I do not understand the structure of a CActiveDataProvider or how to access it. It appears from the class reference that the getData method should help, but I cannot get meaningful data out of that either.
Please somebody explain it to me.
Also, when you create a dataprovider, then pass it to a CGridView, when is the actual SQL search run? I gather the view script runs it, but in what method?
Thank you in advance to anyone who takes the time to explain all this to me.
#2
Posted 02 June 2012 - 08:18 PM
CActiveDataProvider is mostly used for displaying purpose together with a CGridView or a CListView.
It's a kind of "Search Query Executor" that returns an array of db records responding to a request from a CGridView or a CListView.
We usually don't have to trigger the query execution by ourselves. And we don't care much about the returned array. Those things are for the grid or the list to take care of. We only have to write the code for the grid or the list to display each single data row.
And, we usually do not use CActiveDataProvider to UPDATE db records.
I would design your use case as a series of several phases.
1) Initial page display
This is done by a controller action on the server.
It should produce a HTML output and send it to the user's browser.
We will use CActiveDataProvider and CGridView to display the page that shows all the "Red" balls.
2) Selection of the balls
This should happen on the user's browser.
The user will select the balls that he wants to change color from "Red" to "Orange", for example.
The selection can be represented by a check box on each row, and you have to wrap the grid with a form.
3) Submission of the selection to the server
The user will press the 'Change Color to Orange' button.
The browser will submit a form with the selected ids of balls.
4) Receive the submission and update the db
This is on the server side again.
We will receive the selected ids of the balls and save changes to the db.
foreach($selected_ids as $id)
{
$model = Ball::model()->findByPk($id);
$model->color = "Orange";
$model->save();
}
Note that we don't need CActiveDataProvider here.
5) Update page display
We will use CActiveDataProvider and CGridView once again to display the updated page. It's just the same with phase 1).
Well, does it make some sense?
#3
Posted 02 June 2012 - 08:41 PM
softark, on 02 June 2012 - 08:18 PM, said:
Well, does it make some sense?
Yes, thank you. I've been having a hard time figuring out how to reference the dataprovider. What does it belong to? I went into the source code, and it looks like at the top level you can use $this->dataProvider, and in lower levels, use $this->grid->dataProvider.
Your explanation will be extremely useful to me, and I appreciate the time you took.
#4
Posted 02 June 2012 - 09:06 PM
fpolli, on 02 June 2012 - 08:41 PM, said:
Um, yes. When "$this" is a grid, then "$this->dataProvider" is the data provider, and when "$this" is a data column in a grid, then "$this->grid->dataProvider" is it.
CBaseListView::dataProvider
CGridColumn::grid
Accustom yourself to look up the reference. I assure you that it will be a great fun.
BTW, why do you want to access the data provider from the grid or the column?
#5
Posted 02 June 2012 - 09:11 PM
softark, on 02 June 2012 - 09:06 PM, said:
CBaseListView::dataProvider
CGridColumn::grid
Accustom yourself to look up the reference. I assure you that it will be a great fun.
BTW, why do you want to access the data provider from the grid or the column?
I've already looked at the class reference many, many times, but it is hard to understand when you don't already understand it. When I went to the source files, I started to figure it out. You explain things a lot more simply than they are in the class reference.
I wanted to pass a list of ids of all the records to a controller function.
#6
Posted 02 June 2012 - 10:23 PM
fpolli, on 02 June 2012 - 09:11 PM, said:
Yeah, that's true in a sense, indeed.
Until you've got a big picture of the framework in mind, it's a little bit frustrating. I also had a hard time when I started learning Yii.
Especially the grid and the data provider were the complete mystery to me. I mean the gii-generated admin page. I couldn't understand why it could work like that.
Don't worry.
Quote
I see.
But on which phase of the use case do you want to use the list? And for what reason and how?
#7
Posted 03 June 2012 - 02:37 PM
softark, on 02 June 2012 - 10:23 PM, said:
Until you've got a big picture of the framework in mind, it's a little bit frustrating. I also had a hard time when I started learning Yii.
Especially the grid and the data provider were the complete mystery to me. I mean the gii-generated admin page. I couldn't understand why it could work like that.
Don't worry.
I see.
But on which phase of the use case do you want to use the list? And for what reason and how?
Yes, the grid and dataprovider were a mystery to me until last night when I read your post and the source of CGridView and its parents. Now it is just like my wife -- I understand a lot and know how to recognize certain behaviors, but still partially a mystery!
Basically I wanted to pass the list of all ids, and the list of selected ids from the view to the controller, then strike the selected from the overall list and generate a new record set. But having thought it through I have gone in a bit of a different direction, because pagination with the gridview screws everything up.
Thank you for your help. It's nice to see a forum where more people are interested in helping than in making people feel stupid.
#8
Posted 03 June 2012 - 06:48 PM
OK, why don't you post a picture of your sweeeet wife?
#9
Posted 04 June 2012 - 03:41 AM
Anyway, this wiki should also help. It creates several dataproviders in different scenarios and it also inspects an array of data received from a Dropdownlist in the view (Another use case - Child-grid in create and update actions).
http://www.yiiframew...ew-was-clicked/

Help











