Yii Framework Forum: Ch 6 - Note about exit exercise view/issue/update.php - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

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

#1 User is offline   Bob Monteverde 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 4
  • Joined: 06-November 10
  • Location:New York

Posted 06 November 2010 - 06:19 PM

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

#2 User is offline   Daniel Schweiger 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 1
  • Joined: 16-December 10

Posted 16 December 2010 - 09:46 AM

just went into this issue ... and now solved :-) thx
0

#3 User is offline   rifferte 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 2
  • Joined: 04-November 10
  • Location:Long Island, NY

Posted 31 December 2010 - 10:19 PM

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.
0

#4 User is offline   cephyn 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 16
  • Joined: 11-March 11

Posted 19 May 2011 - 04:25 PM

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.
0

#5 User is offline   HomanXH 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 15
  • Joined: 05-June 11

Posted 18 June 2011 - 01:21 AM

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:

Quote

...
$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, 'pid'=>$model->project->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)
),
);
...

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

Quote

...
$this->menu=array(
array('label'=>'Create Issue', 'url'=>array('create', 'pid'=>$this->getProject()->id)),
array('label'=>'Manage Issue', 'url'=>array('admin', 'pid'=>$this->getProject()->id)),
);
...

0

#6 User is offline   murphaph 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 29
  • Joined: 06-May 11

Posted 06 July 2011 - 03:18 PM

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!
0

#7 User is offline   iansane 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 13
  • Joined: 04-November 11

Posted 07 November 2011 - 07:07 PM

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.
0

#8 User is offline   Tekato 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 9
  • Joined: 11-November 11

Posted 14 November 2011 - 07:19 AM

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 ;)
0

#9 User is offline   yasen 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 32
  • Joined: 29-October 11
  • Location:Sofia, Bulgaria

Posted 18 December 2011 - 06:07 AM

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

0

#10 User is offline   Rafael Lima 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 18
  • Joined: 30-January 12

Posted 06 February 2012 - 11:15 AM

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

#11 User is offline   mawey 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 1
  • Joined: 17-October 12

Posted 17 October 2012 - 10:29 AM

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

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'));
}


Can anyone help me?

Thanks in advance.

mawey
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users