Ch 6 - Note about exit exercise view/issue/update.php

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:

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)

Thanks for this. I'm struggling to learn Yii, and I looked at the other threads and didn't even really understand the "incorrect" solution, especially when it didn't work right away. This seems like even more voodoo magic, but it works, so I'm hoping I can study it and understand it. Someday.

I just ran into this. Would be nice if when the book gives exercises it would at least tell which files in the views/issues need to be updated so I'm wondering if I got them all and I guess I'll find out if one of the links doesn't work right.

But I was unaware of the update issue problem as I hadn't tested it yet.

Hy all, very new @ OOP php and Yii framework. I like it alot but the book has a lot of problems. One of witch is writen partially in this thread. If Bob can be so kind or anyone else to help me figure out how to modify all the links for the issue.The code: