Yii Framework Forum: Actionadmin Inside Projectcontroller.php - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Actionadmin Inside Projectcontroller.php

#1 User is offline   Trinh Duy Hung 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 73
  • Joined: 26-October 12

Posted 20 November 2012 - 05:36 AM

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();

2. 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!
0

#2 User is offline   CeBe 

  • Advanced Member
  • Yii
  • Group: Yii Dev Team
  • Posts: 486
  • Joined: 16-July 10
  • Location:Berlin. Germany

Posted 20 November 2012 - 06:14 AM

'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.
0

#3 User is offline   luc 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 211
  • Joined: 22-June 10
  • Location:france

Posted 20 November 2012 - 07:23 AM

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.
Hey Ho !
Let's go !
0

#4 User is offline   Trinh Duy Hung 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 73
  • Joined: 26-October 12

Posted 20 November 2012 - 08:34 PM

Thank you guys!

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

Cheers!
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users