Use Gii and produce issue model file as follow.
<?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 TblUser $owner
* @property TblProject $project
* @property TblUser $requester
*/
class Issue extends CActiveRecord
{
/**
* Returns the static model of the specified AR class.
* @param string $className active record class name.
* @return Issue the static model class
*/
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;
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 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(
'owner' => array(self::BELONGS_TO, 'TblUser', 'owner_id'),
'project' => array(self::BELONGS_TO, 'TblProject', 'project_id'),
'requester' => array(self::BELONGS_TO, 'TblUser', 'requester_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,
));
}
/** Retrieves a list of issue types,return array an array of availabl issue types.*/
public function getTypeOptions()
{
return array(
self::TYPE_BUG=>'Bug',
self::TYPE_FEATURE=>'Feature',
self::TYPE_TASK=>'Task',
);
}
public function getStatusOptions()
{
return array(
self::STATUS_NOT_YET_STARTED=>'Not yet started',
self::STATUS_STARTED=>'Started',
self::STATUS_FINISHED=>'Finished',
);
}
}
function relations is
return array(
'owner' => array(self::BELONGS_TO, 'TblUser', 'owner_id'),
'project' => array(self::BELONGS_TO, 'TblProject', 'project_id'),
'requester' => array(self::BELONGS_TO, 'TblUser', 'requester_id'),
);
and lead to wrong when to follow getUserOptions practise.
PHP warning
include(TblProject.php): failed to open stream: No such file or directory
/var/www/yii/framework/YiiBase.php(423)
411 {
412 include($classFile);
413 if(YII_DEBUG && basename(realpath($classFile))!==$className.'.php')
414 throw new CException(Yii::t('yii','Class name "{class}" does not match class file "{file}".', array(
415 '{class}'=>$className,
416 '{file}'=>$classFile,
417 )));
418 break;
419 }
420 }
421 }
422 else
423 include($className.'.php');
424 }
425 else // class name with namespace in PHP 5.3
426 {
427 $namespace=str_replace('\\','.',ltrim($className,'\\'));
428 if(($path=self::getPathOfAlias($namespace))!==false)
429 include($path.'.php');
430 else
431 return false;
432 }
433 return class_exists($className,false) || interface_exists($className,false);
434 }
435 return true;
Stack Trace
#0
+
/var/www/yii/framework/YiiBase.php(423): YiiBase::autoload()
#1
unknown(0): YiiBase::autoload("TblProject")
#2
+
/var/www/yii/framework/db/ar/CActiveRecord.php(387): spl_autoload_call("TblProject")
#3
+
/var/www/yii/framework/db/ar/CActiveFinder.php(220): CActiveRecord::model("TblProject")
#4
+
/var/www/yii/framework/db/ar/CActiveFinder.php(49): CActiveFinder->buildJoinTree(CJoinElement, "project")
#5
+
/var/www/yii/framework/db/ar/CActiveRecord.php(272): CActiveFinder->__construct(Issue, "project")
#6
+
/var/www/yii/framework/db/ar/CActiveRecord.php(142): CActiveRecord->getRelated("project")
#7
–
/var/www/trackstar/protected/views/issue/_form.php(44): CActiveRecord->__get("project")
39 <?php echo $form->error($model,'status_id'); ?>
40 </div>
41
42 <div class="row">
43 <?php echo $form->labelEx($model,'owner_id'); ?>
44 <?php echo $form->dropDownList($model,'owner_id', $model->project->getUserOptions()); ?>
45 <?php echo $form->error($model,'owner_id'); ?>
46 </div>
47
48 <div class="row">
49 <?php echo $form->labelEx($model,'requester_id'); ?>
#8
+
/var/www/yii/framework/web/CBaseController.php(127): require("/var/www/trackstar/protected/views/issue/_form.php")
#9
+
/var/www/yii/framework/web/CBaseController.php(96): CBaseController->renderInternal("/var/www/trackstar/protected/views/issue/_form.php", array("model" => Issue), true)
#10
+
/var/www/yii/framework/web/CController.php(870): CBaseController->renderFile("/var/www/trackstar/protected/views/issue/_form.php", array("model" => Issue), true)
#11
–
/var/www/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)); ?>
#12
+
/var/www/yii/framework/web/CBaseController.php(127): require("/var/www/trackstar/protected/views/issue/create.php")
#13
+
/var/www/yii/framework/web/CBaseController.php(96): CBaseController->renderInternal("/var/www/trackstar/protected/views/issue/create.php", array("model" => Issue), true)
#14
+
/var/www/yii/framework/web/CController.php(870): CBaseController->renderFile("/var/www/trackstar/protected/views/issue/create.php", array("model" => Issue), true)
#15
+
/var/www/yii/framework/web/CController.php(783): CController->renderPartial("create", array("model" => Issue), true)
#16
–
/var/www/trackstar/protected/controllers/IssueController.php(82): CController->render("create", array("model" => Issue))
77 $this->redirect(array('view','id'=>$model->id));
78 }
79
80 $this->render('create',array(
81 'model'=>$model,
82 ));
83 }
84
85 /**
86 * Updates a particular model.
87 * If update is successful, the browser will be redirected to the 'view' page.
#17
+
/var/www/yii/framework/web/actions/CInlineAction.php(50): IssueController->actionCreate()
#18
+
/var/www/yii/framework/web/CController.php(309): CInlineAction->runWithParams(array("r" => "issue/create", "pid" => "2"))
#19
+
/var/www/yii/framework/web/filters/CFilterChain.php(134): CController->runAction(CInlineAction)
#20
+
/var/www/trackstar/protected/controllers/IssueController.php(199): CFilterChain->run()
#21
+
/var/www/yii/framework/web/filters/CInlineFilter.php(59): IssueController->filterProjectContext(CFilterChain)
#22
+
/var/www/yii/framework/web/filters/CFilterChain.php(131): CInlineFilter->filter(CFilterChain)
#23
+
/var/www/yii/framework/web/filters/CFilter.php(41): CFilterChain->run()
#24
+
/var/www/yii/framework/web/CController.php(1146): CFilter->filter(CFilterChain)
#25
+
/var/www/yii/framework/web/filters/CInlineFilter.php(59): CController->filterAccessControl(CFilterChain)
#26
+
/var/www/yii/framework/web/filters/CFilterChain.php(131): CInlineFilter->filter(CFilterChain)
#27
+
/var/www/yii/framework/web/CController.php(292): CFilterChain->run()
#28
+
/var/www/yii/framework/web/CController.php(266): CController->runActionWithFilters(CInlineAction, array("accessControl", "projectContext + create"))
#29
+
/var/www/yii/framework/web/CWebApplication.php(283): CController->run("create")
#30
+
/var/www/yii/framework/web/CWebApplication.php(142): CWebApplication->runController("issue/create")
#31
+
/var/www/yii/framework/base/CApplication.php(162): CWebApplication->processRequest()
#32
+
/var/www/trackstar/index.php(13): CApplication->run()
2013-10-09 14:38:58 Apache/2.2.22 (Ubuntu) Yii Framework/1.1.12
change function relations array as
return array(
'owner' => array(self::BELONGS_TO, 'User', 'owner_id'),
'project' => array(self::BELONGS_TO, 'Project', 'project_id'),
'requester' => array(self::BELONGS_TO, 'User', 'requester_id'),
);
is ok.
But who know how to produce correct function relations in advance?