I’m still getting my bearings around Yii, but here’s a small thing I noticed (and how I fixed it), both trying to share my experience, and to see if my ‘solution’ is what should be done.
So, I made it to the end of Ch6 last night, half a sleep. Waited to the morning to complete the exercise at the end:
'There are several places throughout the view files under /protected/
views/issues/ that contain links that require a pid querystring
to be added in order to work properly. We leave it as an exercise to the
reader to make the appropriate changes following the same approach
as provided in these examples. As we proceed with our application’s
development, we’ll assume all links to create a new issue or to display
a list of issues are properly formatted to contain the appropriate pid
querystring parameter.’
Before jumping in (to what appears to be a relatively easy exercise repeating some stuff that was already done) I went to test functionality at my current state to identify where I needed to add the pid. Right away I realized a problem with issue/update (/localhost/trackstar/index.php?r=issue/update&id=1).
Only some HTML from view/issue/_form.php was showing up, and the rest of the page HTML was omitted, as well as NO error displayed. Based on where the HTML stopped rendering, I realized the problem was stemming from:
<?php echo $form->dropDownList($model,'owner_id', $this->getProject()->getUserOptions()); ?>
Of course I had to back track through my work to make sure _form.php was working when it was supposed to in issue/create. Without thinking much, I looked at actionCreate in the IssueController, and saw the additional (with respect to actionUpdate) line:
$model->project_id = $this->_project->id;
Now, simply adding this to the actionUpdate was NOT the solution (now there was an error to be seen)… I’ll skip a few steps in my thought process here to jump to why actionCreate works and not actionUpdate… as you may remember, loadProject() is called in filterProjectContext() which is executed for create, index, & admin in filters().
THE "INCORRECT" SOLUTION
My first thought was to add update to the filters to execute filterProjectContext(), and add a pid to the issue/update links. Sure this works… BUT if an issue is already made, why would you need to explicitly state the pid via $_GET or $_POST. So, like in many programming situations, A solution to the problem isn’t always the CORRECT solution to a problem.
THE "CORRECT" SOLUTION
Instead of explicitly giving the pid via $_GET, I added this line following the loadModule line in IssueController’s actionUpdate method:
$this->loadProject($model->project->id);
To sum up this solution, every Issue (already made) already has a project_id. There’s no need to give it this information again, AND in fact you will be executing more code to get to the end result, which is simply the above line of code.
Here’s my complete IssueController/actionUpdate for those that just wanna copy and paste the whole thing:
/**
* 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);
$this->loadProject($model->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('update',array(
'model'=>$model,
));
}
Anyway, like I said, I’m still very new to Yii. So please correct me if something I am doing is wrong.
**On a side note, I am using the current dev version of yii (which may explain the slightly different code generated by Gii if anyone is confused)