Chapter 5 Web Applications With Yii And Php Getuseroptions

im stucked in making the trackstar app. I can’t find solutions to other sections so i decided to post it here. I hope someone helps me

Here’s my IssueController located at : /protected/controllers/IssueController.php


<?php


class IssueController extends Controller

{





/**

*@ @var private property containing the associated Project model instance.

*/


	private $_project = null;


	/**

	* Protected method to load the associated Project model class

	* @param integer projectId the primary identifier of the associated Project

	* @return object the Project data model based on the primary key */


	protected function loadProject($projectId) {

		//if the project property is null,create it based on input Id

		if($this->_project==null){

			$this->_project=Project::model()->findByPk($projectId);

			if($this->_project===null){

				throw new CHttpException(404,'The requested project does not exist.');

			}

		}


		return $this->_project;

	}


	/**

	* In-class defined filter method, configured for use in the above filters()

	*method. It is called before the actionCreate() action method is run in

	*order to ensure a proper project context

	*/

	/**

	*Implementing Filters

	*/




	public function filterProjectContext($filterChain)

	{

	//set the project identifier based on GET input request variables

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

		$this->loadProject($_GET['pid']); 

	else

		throw new CHttpException(403,'Must specify a project before performing this action');

	//complete the running of other filters and execute the requested action

		$filterChain->run();

	}


	/**

	 * @var string the default layout for the views. Defaults to '//layouts/column2', meaning

	 * using two-column layout. See 'protected/views/layouts/column2.php'.

	 */

	public $layout='//layouts/column2';


	/**

	 * @return array action filters

	 */

	public function filters()

	{

		return array(

			'accessControl', // perform access control for CRUD operations

			'projectContext + create update', // check to ensure valid project context

			);

	}


	/**

	 * Specifies the access control rules.

	 * This method is used by the 'accessControl' filter.

	 * @return array access control rules

	 */

	public function accessRules()

	{

		return array(

			array('allow',  // allow all users to perform 'index' and 'view' actions

				'actions'=>array('index','view'),

				'users'=>array('*'),

			),

			array('allow', // allow authenticated user to perform 'create' and 'update' actions

				'actions'=>array('create','update'),

				'users'=>array('@'),

			),

			array('allow', // allow admin user to perform 'admin' and 'delete' actions

				'actions'=>array('admin','delete'),

				'users'=>array('admin'),

			),

			array('deny',  // deny all users

				'users'=>array('*'),

			),

		);

	}


	/**

	 * Displays a particular model.

	 * @param integer $id the ID of the model to be displayed

	 */

	public function actionView($id)

	{

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

			'model'=>$this->loadModel($id),

		));

	}


	/**

	 * Creates a new model.

	 * If creation is successful, the browser will be redirected to the 'view' page.

	 */

	public function actionCreate()

	{

		$model=new Issue;

		$model->project_id=$this->_project->id;

		// Uncomment the following line if AJAX validation is needed

		// $this->performAjaxValidation($model);


		if(isset($_POST['Issue']))

		{

			$model->attributes=$_POST['Issue'];

			if($model->save())

				$this->redirect(array('view','id'=>$model->id));

		}


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

			'model'=>$model,

		));

	}


	/**

	 * Updates a particular model.

	 * If update is successful, the browser will be redirected to the 'view' page.

	 * @param integer $id the ID of the model to be updated

	 */

	public function actionUpdate($id)

	{

		$model=$this->loadModel($id);


		// Uncomment the following line if AJAX validation is needed

		// $this->performAjaxValidation($model);


		if(isset($_POST['Issue']))

		{

			$model->attributes=$_POST['Issue'];

			if($model->save())

				$this->redirect(array('view','id'=>$model->id));

		}


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

			'model'=>$model,

		));

	}


	/**

	 * Deletes a particular model.

	 * If deletion is successful, the browser will be redirected to the 'admin' page.

	 * @param integer $id the ID of the model to be deleted

	 */

	public function actionDelete($id)

	{

		$this->loadModel($id)->delete();


		// if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser

		if(!isset($_GET['ajax']))

			$this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));

	}


	/**

	 * Lists all models.

	 */

	public function actionIndex()

	{

		$dataProvider=new CActiveDataProvider('Issue');

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

			'dataProvider'=>$dataProvider,

		));

	}


	/**

	 * Manages all models.

	 */

	public function actionAdmin()

	{

		$model=new Issue('search');

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

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

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


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

			'model'=>$model,

		));

	}


	/**

	 * Returns the data model based on the primary key given in the GET variable.

	 * If the data model is not found, an HTTP exception will be raised.

	 * @param integer the ID of the model to be loaded

	 */

	public function loadModel($id)

	{

		$model=Issue::model()->findByPk($id);

		if($model===null)

			throw new CHttpException(404,'The requested page does not exist.');

		return $model;

	}


	/**

	 * Performs the AJAX validation.

	 * @param CModel the model to be validated

	 */

	protected function performAjaxValidation($model)

	{

		if(isset($_POST['ajax']) && $_POST['ajax']==='issue-form')

		{

			echo CActiveForm::validate($model);

			Yii::app()->end();

		}

	}

}



the Issue View Page located at : /protected/views/issue/_form.php

There seems to be a big problem at the getUserOptions located on line 45 that indicated this:

$model->project->getUserOptions()


<?php

/* @var $this IssueController */

/* @var $model Issue */

/* @var $form CActiveForm */

?>


<div class="form">


<?php $form=$this->beginWidget('CActiveForm', array(

	'id'=>'issue-form',

	'enableAjaxValidation'=>false,

)); ?>


	<p class="note">Fields with <span class="required">*</span> are required.</p>


	<?php echo $form->errorSummary($model); ?>


	<div class="row">

		<?php echo $form->labelEx($model,'name'); ?>

		<?php echo $form->textField($model,'name',array('size'=>60,'maxlength'=>255)); ?>

		<?php echo $form->error($model,'name'); ?>

	</div>


	<div class="row">

		<?php echo $form->labelEx($model,'description'); ?>

		<?php echo $form->textArea($model,'description',array('rows'=>6, 'cols'=>50)); ?>

		<?php echo $form->error($model,'description'); ?>

	</div>


<?php /* form for the Type */ ?>

	<div class="row">

		<?php echo $form->labelEx($model,'type_id'); ?>

		<?php echo $form->dropDownList($model,'type_id', $model->getTypeOptions()); ?>

		<?php echo $form->error($model,'type_id'); ?>

	</div>


	<div class="row">

		<?php echo $form->labelEx($model,'status_id'); ?>

		<?php echo $form->dropDownList($model,'status_id', $model->getStatusOptions()); ?>

		<?php echo $form->error($model,'status_id'); ?>

	</div>


	<div class="row">

		<?php echo $form->labelEx($model,'owner_id'); ?>

		<?php echo $form ->dropDownList($model,'owner_id',$model->project->getUserOptions()); ?>

		<?php echo $form->error($model,'owner_id'); ?>

	</div>


	<div class="row">

		<?php echo $form->labelEx($model,'requester_id'); ?>

		<?php echo $form->dropDownList($model,'requester_id',$model->project->getUserOptions()); ?>

		<?php echo $form->error($model,'requester_id'); ?>

	</div>


	<div class="row">

		<?php echo $form->labelEx($model,'create_time'); ?>

		<?php echo $form->textField($model,'create_time'); ?>

		<?php echo $form->error($model,'create_time'); ?>

	</div>


	<div class="row">

		<?php echo $form->labelEx($model,'create_user_id'); ?>

		<?php echo $form->textField($model,'create_user_id'); ?>

		<?php echo $form->error($model,'create_user_id'); ?>

	</div>


	<div class="row">

		<?php echo $form->labelEx($model,'update_time'); ?>

		<?php echo $form->textField($model,'update_time'); ?>

		<?php echo $form->error($model,'update_time'); ?>

	</div>


	<div class="row">

		<?php echo $form->labelEx($model,'update_user_id'); ?>

		<?php echo $form->textField($model,'update_user_id'); ?>

		<?php echo $form->error($model,'update_user_id'); ?>

	</div>


	<div class="row buttons">

		<?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>

	</div>


<?php $this->endWidget(); ?>


</div><!-- form -->

and lastly heres my Issue model located at : /protected/models/Issue.php


<?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

 *

 * The followings are the available model relations:

 * @property User $requester

 * @property User $owner

 * @property Project $id0

 */

class Issue extends CActiveRecord

{

	//add three constant definitions on Web App with Yii and PHP

	

	const TYPE_BUG=0;

	const TYPE_FEATURE=1;

	const TYPE_TASK=2;


	public function getProject()

	{

		

	}

	/**

	  * Retrieves a list of issue types

	  * @return array an array of available issue types.

	  */


	public function getTypeOptions()

	{

		return array(

			self::TYPE_BUG=>'Bug',

			self::TYPE_FEATURE=>'Feature',

			self::TYPE_TASK=>'Task',

			);

	}


	//add three constant variables for status dropdowns


	const STATUS_NYS=0;

	const STATUS_S=1;

	const STATUS_F=2;


	//Retrieves a list of status types


	public function getStatusOptions()

	{

		return array(

			self::STATUS_NYS=>'Not Yet Started',

			self::STATUS_S=>'Started',

			self::STATUS_F=>'Finished',

			);

	}

		

	/**

	 * Returns the static model of the specified AR class.

	 * @param string $className active record class name.

	 * @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'=>255),

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

			//the CRangeValidator rule for Type

			array('type_id', 'in', 'range'=>self::getAllowedTypeRange()),

			//the CRangeValidator rule for Status

			array('type_id', 'in', 'range'=>self::getAllowedStatusRange()),

			// 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 a method to return allowed range for type

	*/

	

	public static function getAllowedTypeRange()

		{

			return array(

			self::STATUS_NYS,

			self::STATUS_S,

			self::STATUS_F,

		);

	}


	/**

	*@return a method to return allowed range for type

	*/


	public static function getAllowedStatusRange()

		{

			return array(

			self::TYPE_BUG,

			self::TYPE_FEATURE,

			self::TYPE_TASK,

		);

	}

	/**

	 * @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'),

			'id0' => array(self::BELONGS_TO, '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($this, array(

			'criteria'=>$criteria,

		));

	}

}

Is there an error you get or what’s the exact problem?

What looks strange to me is this call:


$model->project 

Usually you call model relation this way, but there’s not such relation at the model. You have $_project defined at the controller, so, if you have getUserOptions() method at Project model, then, I suppose you should rather have (not tested) something like


$this->loadProject()->getUserOptions()

$this stands for IssueController here.

Hope it’ll help. Or post the error you get first of all.

Hi thank you for your reply

here’s what im getting:

Property "Issue.project" is not defined





D:\xampp\htdocs\trackstar\framework\db\ar\CActiveRecord.php(144)


132      */

133     public function __get($name)

134     {

135         if(isset($this->_attributes[$name]))

136             return $this->_attributes[$name];

137         else if(isset($this->getMetaData()->columns[$name]))

138             return null;

139         else if(isset($this->_related[$name]))

140             return $this->_related[$name];

141         else if(isset($this->getMetaData()->relations[$name]))

142             return $this->getRelated($name);

143         else

144             return parent::__get($name);

145     }

146 

147     /**

148      * PHP setter magic method.

149      * This method is overridden so that AR attributes can be accessed like properties.

150      * @param string $name property name

151      * @param mixed $value property value

152      */

153     public function __set($name,$value)

154     {

155         if($this->setAttribute($name,$value)===false)

156         {


Stack Trace

#0 	

+

 D:\xampp\htdocs\trackstar\framework\db\ar\CActiveRecord.php(144): CComponent->__get("project")

#1 	

–

 D:\xampp\htdocs\trackstar\protected\views\issue\_form.php(45): CActiveRecord->__get("project")


40         <?php echo $form->error($model,'status_id'); ?>

41     </div>

42 

43     <div class="row">

44         <?php echo $form->labelEx($model,'owner_id'); ?>

45         <?php echo $form->dropDownList($model,'owner_id',$model->project->getUserOptions()); ?>

46         <?php echo $form->error($model,'owner_id'); ?>

47     </div>

48 

49     <div class="row">

50         <?php echo $form->labelEx($model,'requester_id'); ?>


#2 	

+

 D:\xampp\htdocs\trackstar\framework\web\CBaseController.php(127): require("D:\xampp\htdocs\trackstar\protected\views\issue\_form.php")

#3 	

+

 D:\xampp\htdocs\trackstar\framework\web\CBaseController.php(96): CBaseController->renderInternal("D:\xampp\htdocs\trackstar\protected\views\issue\_form.php", array("model" => Issue), true)

#4 	

+

 D:\xampp\htdocs\trackstar\framework\web\CController.php(870): CBaseController->renderFile("D:\xampp\htdocs\trackstar\protected\views\issue\_form.php", array("model" => Issue), true)

#5 	

–

 D:\xampp\htdocs\trackstar\protected\views\issue\create.php(18): CController->renderPartial("_form", array("model" => Issue))


13 );

14 ?>

15 

16 <h1>Create Issue</h1>

17 

18 <?php echo $this->renderPartial('_form', array('model'=>$model)); ?>


#6 	

+

 D:\xampp\htdocs\trackstar\framework\web\CBaseController.php(127): require("D:\xampp\htdocs\trackstar\protected\views\issue\create.php")

#7 	

+

 D:\xampp\htdocs\trackstar\framework\web\CBaseController.php(96): CBaseController->renderInternal("D:\xampp\htdocs\trackstar\protected\views\issue\create.php", array("model" => Issue), true)

#8 	

+

 D:\xampp\htdocs\trackstar\framework\web\CController.php(870): CBaseController->renderFile("D:\xampp\htdocs\trackstar\protected\views\issue\create.php", array("model" => Issue), true)

#9 	

+

 D:\xampp\htdocs\trackstar\framework\web\CController.php(783): CController->renderPartial("create", array("model" => Issue), true)

#10 	

–

 D:\xampp\htdocs\trackstar\protected\controllers\IssueController.php(126): CController->render("create", array("model" => Issue))


121                 $this->redirect(array('view','id'=>$model->id));

122         }

123 

124         $this->render('create',array(

125             'model'=>$model,

126         ));

127     }

128 

129     /**

130      * Updates a particular model.

131      * If update is successful, the browser will be redirected to the 'view' page.


#11 	

+

 D:\xampp\htdocs\trackstar\framework\web\actions\CInlineAction.php(50): IssueController->actionCreate()

#12 	

+

 D:\xampp\htdocs\trackstar\framework\web\CController.php(309): CInlineAction->runWithParams(array("r" => "issue/create", "pid" => "1"))

#13 	

+

 D:\xampp\htdocs\trackstar\framework\web\filters\CFilterChain.php(134): CController->runAction(CInlineAction)

#14 	

+

 D:\xampp\htdocs\trackstar\protected\controllers\IssueController.php(49): CFilterChain->run()

#15 	

+

 D:\xampp\htdocs\trackstar\framework\web\filters\CInlineFilter.php(59): IssueController->filterProjectContext(CFilterChain)

#16 	

+

 D:\xampp\htdocs\trackstar\framework\web\filters\CFilterChain.php(131): CInlineFilter->filter(CFilterChain)

#17 	

+

 D:\xampp\htdocs\trackstar\framework\web\filters\CFilter.php(41): CFilterChain->run()

#18 	

+

 D:\xampp\htdocs\trackstar\framework\web\CController.php(1146): CFilter->filter(CFilterChain)

#19 	

+

 D:\xampp\htdocs\trackstar\framework\web\filters\CInlineFilter.php(59): CController->filterAccessControl(CFilterChain)

#20 	

+

 D:\xampp\htdocs\trackstar\framework\web\filters\CFilterChain.php(131): CInlineFilter->filter(CFilterChain)

#21 	

+

 D:\xampp\htdocs\trackstar\framework\web\CController.php(292): CFilterChain->run()

#22 	

+

 D:\xampp\htdocs\trackstar\framework\web\CController.php(266): CController->runActionWithFilters(CInlineAction, array("accessControl", "projectContext + create index admin"))

#23 	

+

 D:\xampp\htdocs\trackstar\framework\web\CWebApplication.php(283): CController->run("create")

#24 	

+

 D:\xampp\htdocs\trackstar\framework\web\CWebApplication.php(142): CWebApplication->runController("issue/create")

#25 	

+

 D:\xampp\htdocs\trackstar\framework\base\CApplication.php(162): CWebApplication->processRequest()

#26 	

+

 D:\xampp\htdocs\trackstar\index.php(13): CApplication->run() 

So yes, seems I was right about how you should call users options. Pls take a look at my previous message.