Agile Yii Book Chapter 6

So… I actually have several questions. But I’m following along the Agile Yii book and I came across stuff that isn’t explained.

I know this might be vague but maybe not.

In chapter 6, there’s a Issue Model. We added this function:


	public function getStatusText()

	{

	    $statusOptions = $this->statusOptions;

	    return isset($statusOptions[$this->status_id]) ?

	        $statusOptions[$this->status_id] : "unknown status ({$this->status_id})";

	}

Ok, the confusion is this line:


	    $statusOptions = $this->statusOptions;

statusOptions is not a class property. It’s just declared in this method and it’s referring to itself? Is that what’s going on? Some how Yii knows that statusOptions is of object Issue Model and it now represent the Issue table. Cause we can access Issue fields and return stuff. Am I getting this right?

Edit:

To Balrok, ah my bad, here’s the code:


<?php


/**

 * This is the model class for table "tbl_issue".

 *

 * The followings are the available columns in table 'tbl_issue':

 * @property integer $id

 * @property string $name

 * @property string $description

 * @property integer $project_id

 * @property integer $type_id

 * @property integer $status_id

 * @property integer $owner_id

 * @property integer $requester_id

 * @property string $create_time

 * @property integer $create_user_id

 * @property string $update_time

 * @property integer $update_user_id

 */

class Issue extends CActiveRecord

{

    const TYPE_BUG = 0;

    const TYPE_FEATURE = 1;

    const TYPE_TASK = 2;

    

    const STATUS_NOT_YET_STARTED = 0;

    const STATUS_STARTED = 1;

    const STATUS_FINISHED = 2;

    

	/**

	 * Returns the static model of the specified AR class.

	 * @return Issue the static model class

	 */

	public static function model($className=__CLASS__)

	{

		return parent::model($className);

	}


	/**

	 * @return string the associated database table name

	 */

	public function tableName()

	{

		return 'tbl_issue';

	}


	/**

	 * @return array validation rules for model attributes.

	 */

	public function rules()

	{

		// NOTE: you should only define rules for those attributes that

		// will receive user inputs.

		return array(

			array('name', 'required'),

			array('project_id, type_id, status_id, owner_id, requester_id, create_user_id, update_user_id', 'numerical', 'integerOnly'=>true),

			array('name', 'length', 'max'=>256),

			array('description', 'length', 'max'=>2000),

			array('create_time, update_time', 'safe'),

			// The following rule is used by search().

			// Please remove those attributes that should not be searched.

			array('id, name, description, project_id, type_id, status_id, owner_id, requester_id, create_time, create_user_id, update_time, update_user_id', 'safe', 'on'=>'search'),

		);

	}


	/**

	 * @return array relational rules.

	 */

	public function relations()

	{

		// NOTE: you may need to adjust the relation name and the related

		// class name for the relations automatically generated below.

		return array(

			'requester' => array(self::BELONGS_TO, 'User', 'requester_id'),

			'owner' => array(self::BELONGS_TO, 'User', 'owner_id'),

			'project' => array(self::BELONGS_TO, 'Project', 'project_id'),

		);

	}


	/**

	 * @return array customized attribute labels (name=>label)

	 */

	public function attributeLabels()

	{

		return array(

			'id' => 'ID',

			'name' => 'Name',

			'description' => 'Description',

			'project_id' => 'Project',

			'type_id' => 'Type',

			'status_id' => 'Status',

			'owner_id' => 'Owner',

			'requester_id' => 'Requester',

			'create_time' => 'Create Time',

			'create_user_id' => 'Create User',

			'update_time' => 'Update Time',

			'update_user_id' => 'Update User',

		);

	}


	/**

	 * Retrieves a list of models based on the current search/filter conditions.

	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.

	 */

	public function search()

	{

		// Warning: Please modify the following code to remove attributes that

		// should not be searched.


		$criteria=new CDbCriteria;


		$criteria->compare('id',$this->id);


		$criteria->compare('name',$this->name,true);


		$criteria->compare('description',$this->description,true);


		$criteria->compare('project_id',$this->project_id);


		$criteria->compare('type_id',$this->type_id);


		$criteria->compare('status_id',$this->status_id);


		$criteria->compare('owner_id',$this->owner_id);


		$criteria->compare('requester_id',$this->requester_id);


		$criteria->compare('create_time',$this->create_time,true);


		$criteria->compare('create_user_id',$this->create_user_id);


		$criteria->compare('update_time',$this->update_time,true);


		$criteria->compare('update_user_id',$this->update_user_id);


		return new CActiveDataProvider(get_class($this), array(

			'criteria'=>$criteria,

		));

	}

	

	/**

	 * @return array issue type names indexed by type IDs

	 */

	public function getTypeOptions()

	{

	    return array(

	        self::TYPE_BUG => 'Bug',

	        self::TYPE_FEATURE => 'Feature',

	        self::TYPE_TASK => 'Task'

	    );

	}

	

	/**

	 * @return array issue status names indexed by status IDs

	 */

	public function getStatusOptions()

	{

	    return array(

	        self::STATUS_NOT_YET_STARTED => 'Not yet started',

	        self::STATUS_STARTED => 'Started',

	        self::STATUS_FINISHED => 'Finished'

	    );

	}

	

	/**

	 * @return string the status text display for the current issues

	 */

	public function getStatusText()

	{

	    $statusOptions = $this->statusOptions;

	    return isset($statusOptions[$this->status_id]) ?

	        $statusOptions[$this->status_id] : "unknown status ({$this->status_id})";

	}

	

	/**

	 * @return string the type text display for the current issue

	 */

	public function getTypeText()

	{

	    $typeOptions = $this->typeOptions;

	    return isset($typeOptions[$this->type_id]) ?

	        $typeOptions[$this->type_id] : "unknown type ({$this->type_id})";

	}

}

perhaps you link the whole code… but in php you have to explicitly declare $this when you refer to a class-property

so $statusOptions is not a classProperty it’s just the copy of $this->statusOptions

it just looks like he made the copy to avoid the usage of $this-> and shorten the code

Ok, now since it is a local variable, within the scope of the function that it is defined, how is it that statusOptions can represent the Issue table?

Does Yii just know? So say that I create any local variables within any function, unless defining anything for that said variable, all local variable by default represent Issue table?

If so, then ok. That’s all I’m curious about, since it’s a bit implicit. I thought it would be more explicit:


$local_var = $this->model();

or


$local_var = $this->Issue::model(); 


$this->statusOptions

is actually equivalent to


$this->getStatusOptions()

And that method is defined in the model.

All components in Yii extend from CComponent. If you try to access a property which does not exist, the underlying CComponent checks if there’s a getter method. If that’s the case the method gets called and the results will get returned.

thanks Y!! didn’t know about that feature…

i’ve mostly seen it just as yii-magic :)

Ah thank you very much!! I just figured this out when I tried to do a unit test on a new function with a local variable.

This confused me too. I couldn’t find this feature by searching the official documentation. The getter method makes it look simpler, but increases learning cost.