Actionadmin Inside Projectcontroller.php

Hello everyone!

I’m following the book and stuck in trying understand the actionAdmin in ProjectController.

It says:


public function actionAdmin()

	{

		$model=new Project('search');

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

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

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


		$this->render('admin',array(

			'model'=>$model,

		));

	}

My questions are:

  1. what does

$model=new Project('search');

means? Parameter ‘search’ is used for what in this particular situation?

What if we wrote


$model=new Project();

  1. Why should we do this

$model->unsetAttributes();

We know that Project extends TrackStarActiveRecord( and TrackStarActiveRecord extends CActiveRecord)

So new Project(‘search’) refers to __construct() of CActiveRecord.

With help of api documentation, it says:


public void __construct(string $scenario='insert')

inside __construct I see number of things that I’m not familiar with.

So I want to hear from you.

Cheers!

‘search’ is the scenario the model is used in. The scenario tells which validation Rules should be used on validation and which attributes are considered safe for mass assignment.

There is a rule in your Project model that declares some fields as ‘safe’ on ‘search’. These are the fields that are searchable.

unsetAttributes is called to remove default values to ensure search fields are initially empty.

to complete CeBe response, once you ahve filled some search fields in your admin view, controller will assign them with


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

to perform the search and render the updated admin view.

Thank you guys!

I think I should take a look at how validation rules are applied from "The Denitive Guide to Yii 1.1". But your answers definitely made me feel more understandable.

Cheers!