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:


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

just went into this issue … and now solved :slight_smile: thx

I agree - the solution works.

The implementation seems to be solid - and I agree - why pass the pid via querystring when you have it in the model object already.

Thanks for the heads up with this, now to fix those Issue URLs.

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.

It works. You have to edit view.php, admin.php, create.php, index.php, and update.php files in protected/views/issue

My view.php:

But index.php is different. I have to replace $model->project->id to getProject() because $model is unknown variable, confused me for a while.

Thanks Bob. I am working through the book myself and also wondered how I could update the issue "directly" as all issues have a project ID already. Your solution looks very elegant.

@HomanXH: Perhaps you should reread the (great) post from Bob. He explains quite well why your solution is not really the best one.

Thanks again Bob!

Thanks Bob,

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:




public function actionIndex()

	{

		$dataProvider=new CActiveDataProvider('Issue', array(

		'criteria'=>array(

			'condition'=>'project_id=:projectId',

			'params'=>array(':projectId=>$this->_project->id'),

			),

		));

		

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

			'dataProvider'=>$dataProvider,

		));

	}



And the view




<?php

$this->breadcrumbs=array(

	'Issues'=>array('index','pid'=>$model->project->id),

	$model->name,

);


$this->menu=array(

	array('label'=>'List Issue', 'url'=>array('index', 'pid'=>$model->project->id)),

	array('label'=>'Create Issue', 'url'=>array('create', 'pid'=>$model->project->id)),

	array('label'=>'Update Issue', 'url'=>array('update', 'id'=>$model->id)),

	array('label'=>'Delete Issue', 'url'=>'#', 'linkOptions'=>array('submit'=>array('delete','id'=>$model->id),'confirm'=>'Are you sure you want to delete this item?')),

	array('label'=>'Manage Issue', 'url'=>array('admin', 'pid'=>$model->project->id)),

);

?>



Good news is that every other link works except for the list issues one…i cant make it work and it thors me and cdbc exception :

CDbCommand failed to execute the SQL statement: SQLSTATE[HY093]: Invalid parameter number: parameter was not defined. The SQL statement executed was: SELECT COUNT(*) FROM tbl_issue t WHERE project_id=:projectId

Can someon eplss help me asap if you can…thx ;)

The page "Manage Issues" shows ID-s on the columns Project, Type and Status instead of their string names. The bottom of views/issues/admin.php should be changed from:




'project_id',

'type_id',

'status_id',



to


		

array(

  'name' => 'project_id',

  'value' => 'CHtml::encode($data->project->name)'

),

array(

  'name' => 'type_id',

  'value' => 'CHtml::encode($data->typeText)'

),

array(

  'name' => 'status_id',

  'value' => 'CHtml::encode($data->statusText)'

),



Thanks Bob. Just ran into this problem right now, but it was solved right away thanks to you.

Hello to everybody.

I can both update and delete an issue from the admin view (using the grid) and return to the same grid. It’s ok.

The problem is trying to delete an issue from the “View” of such issue: following the “Delete issue” link, it deletes the register but I can’t manage to go to the list issue page.

I’m checking the Issue::actionDelete and I can’t give a correct link for returning

[i]public function actionDelete($id)

{


	&#036;this-&gt;loadModel(&#036;id)-&gt;delete();





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


	if(&#33;isset(&#036;_GET['ajax']))


		&#036;this-&gt;redirect(isset(&#036;_POST['returnUrl']) ? &#036;_POST['returnUrl'] : array('admin'));


}[/i]

Can anyone help me?

Thanks in advance.

mawey