It looks like you can use search to find all records that match the values you want to search on. In the example listed in the post, they have a form that allows somebody to search a database and the models are returned in the grid widget below. As to rootbeers point, you don’t want somebody to keep entering a password and hoping it returns a match, because then you know their login!
I’ve always used one of the find methods in my app.
But I’m sure you could do something like this:
$model = new Model();
$model->username = 'cnick79';
$dataProvider = $model->search();
Gii-generated ‘search’ method is one of the best things that yii offers you.
I strongly recommend you not to remove it.
CActiveDataProvider is a kind of query executor that you can give it to a CGridView or CListView.
It is not an array of objects like the one that ‘findAll()’ returns. It is an object that can return an array of objects according to the criteria that you specified. And you will not use it directly, but a CGridView or CListView will use it when it shows the items.
Further more, it will modify the search criteria regarding the order and the offset and limit dynamically according to the requests of sorting and pagination from the grid or the list.
You could examine how it is used in gii-generated CRUD.
‘index’ page uses CListView widget, and the widget uses a CActiveDataProvider instance that ‘search’ method has created.
And also the CGridView widget in ‘admin’ page uses a CActiveDataProvider instance that the same ‘search’ method has created.
CActiveDataProvider + (CGridView or CListView) is one of the most powerful tools that Yii has for you, and ‘search’ method in model is a vital part when you want to use it.
Please don’t miss it.
Your can customize ‘search’ method to satisfy your needs, or can have multiple ‘searchXxx’ methods for specific purposes. You can take the gii-generated one just as a skeleton or a template.
I think I have a better understanding of what this method does now.
it searches the loaded properties of current record in the database table, and returns a datastore containing matching records. so if I don’t use the returning datastore at all then I can safely remove the search method. is this correct?
“$this” in “search” method is an instance of the model, but it’s just a holder of the search parameters initially set to empty. It’s not the actual data in the database.
Um, No.
Maybe you are mixing up.
"search" method returns an instance of CActiveDataProvider, not an array of AR objects.
An instance of CActiveDataProvider does provide an array of AR objects.
But usually it’s not you that request it to return the result. Usually CGridView or CListView will do it for you.
If you are sure that you will not use CGridView or CListView for your current model, then I think it’s safe to remove “search” method.