What The Different Between Findall() And Search?

I have : $model=new Model(‘search’) and $model=Model::model()->findAll();

With findAll(), I could call $model[0]->“column_name”. But with ‘search’, it results an error:


Execption: Property "Model.0" is not defined.

 Stack Trace: CModel->offsetGet(0)

So what exactly do findAll() and (‘search’) return?

Pay attention to




$model=Model::model()->findAll();



findAll method return an array, so $model will be an array.

Instead, when you instantiate




$model = new Model("search");



"search" is the scenario, but $model will be the object created by Model class.

This is the misunderstanding.

But they are still a list of models right? So how could I point to object has id=1 or the first model of the list?. $model->0 or must be $model->findByPk($id)?

No, new Model return new instance of Model class.

No, it won’t, instead it return list of models. Because it is from:


	public function actionAdmin() 	{ 

$model=new Model('search'); 		

$model->unsetAttributes();  // clear any default values 		

if(isset($_GET['Model'])) 

$model->attributes=$_GET['Model'];

$this->render('admin',array( 'model'=>$model,)); 	

}



The view in admin.php retrieves $model and render list of all the models. So that’s why I ask how to point to the first object in that list.

The parameter "model" passed to admin view, is not an array,

but is the parameters that will be password to CGridView.

Check carefully admin.php view.

‘Search’ is a scenario and a utility function in the model.

Look at the source for your model.

It is usually generated by Gii.

FindAll, however is a method of CActiveRecord.

Sorry, my mistake. Well, I dont know how this work. Why it must be :$model=new Model(‘search’) but not : $model=new Model()? Because they are the same when we send that parameters to the CGridView. They are a object without value or Null value at its attributes.

And I believe that $_GET[‘Model’] could be returned as array or single object, so “$model->attributes=$_GET[‘Model’]” will assign the attributes and create an array for this $model because php is stateless?

I’ve tried it: Use $model=new Model(); the searching function still working. But not able to search by id, other attributes run very smoothy.

You need to read this:

http://www.yiiframework.com/wiki/266/understanding-scenarios/

:)

It has nothing to do with state(less). Not sure what that means.

The only thing the parameter i [/i]is doing is specifying a scenario. That scenario tells Yii that attributes can be massively assigned - read about it here[size=2] &[/size][size=2] [/size]here.

The findAll and search methods return different things.

[list=1][]$model->search() returns a CActiveDataProvider[]Model::model()->findAll() returns an array of model instances.[/list]

Matt

Thank a lot everyone :)