got stuck at Chapter 6

I got stuck on Page 131 of the book. when i go to this URL,




http://yii/trackstar/index.php?r=project/view&id=2



I’m getting an error, as described at the screen shot at the bottom. but I did followed the instructions

the exact code snippets to move on., what should I do to fix this and move forward ? :unsure:

I guess somwhere in your code you do


$model = $this->loadModel();

Try this instead:


$model = $this->loadModel($_GET['id']);

it didn’t helped :( I checked the errata page of the book from packtpub, there’s none that has something to do that is related in my situation.

here’s the exact code that I got I followed on the book until I got stuck




<?php


class ProjectController extends Controller

{

	/**

	 * @var string the default layout for the views. Defaults to '//layouts/column2', meaning

	 * using two-column layout. See 'protected/views/layouts/column2.php'.

	 */

	public $layout='//layouts/column2';


	/**

	 * @return array action filters

	 */

	public function filters()

	{

		return array(

			'accessControl', // perform access control for CRUD operations

		);

	}


	/**

	 * Specifies the access control rules.

	 * This method is used by the 'accessControl' filter.

	 * @return array access control rules

	 */

	public function accessRules()

	{

		return array(

			array('allow',  // allow all users to perform 'index' and 'view' actions

				'actions'=>array('index','view'),

				'users'=>array('*'),

			),

			array('allow', // allow authenticated user to perform 'create' and 'update' actions

				'actions'=>array('create','update'),

				'users'=>array('@'),

			),

			array('allow', // allow admin user to perform 'admin' and 'delete' actions

				'actions'=>array('admin','delete'),

				'users'=>array('admin'),

			),

			array('deny',  // deny all users

				'users'=>array('*'),

			),

		);

	}


	/**

	 * Displays a particular model.

	 * @param integer $id the ID of the model to be displayed

	 */

	public function actionView($id)

	{

//		$this->render('view',array(

//			'model'=>$this->loadModel($id),

//		));

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

				'criteria' => array(

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

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

				),

				'pagination'=>array(

					'pageSize' => 1,

				),

			));

			

	

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

			 'model' => $this->loadModel(),

			'issueDataProvider' => $issueDataProvider,

		));

	}


	/**

	 * Creates a new model.

	 * If creation is successful, the browser will be redirected to the 'view' page.

	 */

	public function actionCreate()

	{

		$model=new Project;


		// Uncomment the following line if AJAX validation is needed

		// $this->performAjaxValidation($model);


		if(isset($_POST['Project']))

		{

			$model->attributes=$_POST['Project'];

			if($model->save())

				$this->redirect(array('view','id'=>$model->id));

		}


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

			'model'=>$model,

		));

	}


	/**

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


		// Uncomment the following line if AJAX validation is needed

		// $this->performAjaxValidation($model);


		if(isset($_POST['Project']))

		{

			$model->attributes=$_POST['Project'];

			if($model->save())

				$this->redirect(array('view','id'=>$model->id));

		}


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

			'model'=>$model,

		));

	}


	/**

	 * Deletes a particular model.

	 * If deletion is successful, the browser will be redirected to the 'index' page.

	 * @param integer $id the ID of the model to be deleted

	 */

	public function actionDelete($id)

	{

		if(Yii::app()->request->isPostRequest)

		{

			// we only allow deletion via POST request

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

		}

		else

			throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');

	}


	/**

	 * Lists all models.

	 */

	public function actionIndex()

	{

		$dataProvider=new CActiveDataProvider('Project');

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

			'dataProvider'=>$dataProvider,

		));

	}


	/**

	 * Manages all models.

	 */

	public function actionAdmin()

	{

		$model=new Project('search');

		$model->unsetAttributes();  // clear any default values

		if(isset($_GET['Project']))

			$model->attributes=$_GET['Project'];


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

			'model'=>$model,

		));

	}


	/**

	 * Returns the data model based on the primary key given in the GET variable.

	 * If the data model is not found, an HTTP exception will be raised.

	 * @param integer the ID of the model to be loaded

	 */

	public function loadModel($id)

	{

		$model=Project::model()->findByPk((int)$id);

		if($model===null)

			throw new CHttpException(404,'The requested page does not exist.');

		return $model;

	}


	/**

	 * Performs the AJAX validation.

	 * @param CModel the model to be validated

	 */

	protected function performAjaxValidation($model)

	{

		if(isset($_POST['ajax']) && $_POST['ajax']==='project-form')

		{

			echo CActiveForm::validate($model);

			Yii::app()->end();

		}

	}

}




In actionView() method do:




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

                         'model' => $this->loadModel($id),

                        'issueDataProvider' => $issueDataProvider,

                ));




yes, I did this also $this->loadModel($_GET[‘id’]) , based from your first reply, but

still am getting the same error, I tried :(

I really don’t know what’s wrong with it, the error just says “missing argument for ProjectController::loadModel()” at line 59 where line 59 is this part of the actionView()




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



I tried

‘params’ => array(’:projectId’ => $this->loadModel($_GET[‘id’])),

OR

‘params’ => array(’:projectId’ => $this->loadModel($_GET[‘id’]->id,

but when i tried this

‘params’ => array(’:projectId’ => $this->loadModel($_GET[$id]),

and refreshed the same url, the page says

what’s wrong with it ? :(

solved the problem now




	public function actionView($id)

	{

//		$this->render('view',array(

//			'model'=>$this->loadModel($id),

//		));

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

				'criteria' => array(

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

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

				),

				'pagination'=>array(

					'pageSize' => 1,

				),

			));

			

	

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

			 'model' => $this->loadModel($id),

			'issueDataProvider' => $issueDataProvider,

		));

	}



you’re right…and i’ll report this to the errata for the book

If I’m not mistaken, I believe this is due to Gii auto generating different code between 1.1.2 (I think thats what the book is based on) and likely the stable release 1.1.4 (or if you are anal like me, you are using the most recent dev snapshot courtesy of dmtrs’s unofficial git repo).

If you wanted to follow the book 100% step by step, using 1.1.2 for now might be a decent idea, but I think it’s better to have small mistakes for you to figure out and become more involved in the actual development process.

Anyway, good luck, I’m about where you are, starting chapter 7.

Just in case you are wondering, what actually did the trick:

it’s the line


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

In the book there is no $id in loadModel().

Don’t forget to call the loadModel method with the required parameter ‘id’.

This can be found in the stack trace of the debug output, when the error occurs.

Cheers,

Peter

Thanks Friends i got same problem, now [size=“3”]sloved[/size] …thanks you once again friends… :rolleyes:

Thanks for posting the fix. I hit the problem using 1.1.7. I figured there might have been a framework change, but had no real idea how to fix it The problem still doesn’t seem to be in the errata, though they probably don’t consider it a problem if it works with the version used in the book.

Thanks again.

I got the same error as in the original post. However, when I tried the fix that the original poster used, I got more errors

Here’s the code that the OP used to solve the problem




public function actionView($id)

	{

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

			'criteria'=>array(

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

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

		 	),

		 	'pagination'=>array(

		 		'pageSize'=>1,

		 	),

		 ));

		

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

			'model'=>$this->loadModel($id),

			'issueDataProvider'=>$issueDataProvider,

		));




This is the error message I got when I tried the solution


]CException


Issue does not have a method named "getTypeText".


/Applications/MAMP/htdocs/framework/base/CComponent.php(266)


254     public function __call($name,$parameters)

255     {

256         if($this->_m!==null)

257         {

258             foreach($this->_m as $object)

259             {

260                 if($object->getEnabled() && method_exists($object,$name))

261                     return call_user_func_array(array($object,$name),$parameters);

262             }

263         }

264         if(class_exists('Closure', false) && $this->canGetProperty($name) && $this->$name instanceof Closure)

265             return call_user_func_array($this->$name, $parameters);

266         throw new CException(Yii::t('yii','{class} does not have a method named "{name}".',

267             array('{class}'=>get_class($this), '{name}'=>$name)));

268     }

269 

270     /**

271      * Returns the named behavior object.

272      * The name 'asa' stands for 'as a'.

273      * @param string $behavior the behavior name

274      * @return IBehavior the behavior object, or null if the behavior does not exist

275      * @since 1.0.2

276      */

277     public function asa($behavior)

278     {[/b]


	}



And this is the stack trace

[b]Stack Trace

#0

  • /Applications/MAMP/htdocs/framework/db/ar/CActiveRecord.php(213): CComponent->__call("getTypeText", array())

#1

– /Applications/MAMP/htdocs/trackstar/protected/views/issue/_view.php(13): CActiveRecord->__call("getTypeText", array())

08 <?php echo CHtml::encode($data->description); ?>

09 <br />

10

11 <b><?php echo CHtml::encode($data->getAttributeLabel(‘type_id’)); ?>:</b>

12 <?php //echo CHtml::encode($data->type_id); ?>

13 <?php echo CHtml::encode($data->getTypeText()); ?>

14 <br />

15

16 <b><?php echo CHtml::encode($data->getAttributeLabel(‘status_id’)); ?>:</b>

17 <?php //echo CHtml::encode($data->status_id); ?>

18 <?php echo CHtml::encode($data->getStatusText()); ?>

#2

– /Applications/MAMP/htdocs/trackstar/protected/views/issue/_view.php(13): Issue->getTypeText()

08 <?php echo CHtml::encode($data->description); ?>

09 <br />

10

11 <b><?php echo CHtml::encode($data->getAttributeLabel(‘type_id’)); ?>:</b>

12 <?php //echo CHtml::encode($data->type_id); ?>

13 <?php echo CHtml::encode($data->getTypeText()); ?>

14 <br />

15

16 <b><?php echo CHtml::encode($data->getAttributeLabel(‘status_id’)); ?>:</b>

17 <?php //echo CHtml::encode($data->status_id); ?>

18 <?php echo CHtml::encode($data->getStatusText()); ?>[/b]

problem fixed. The solution of OP worked for me, but I had problem in other file.

Hi, I’m reading the book right now.

Instead of loading the model using the ID that is already known, to then get this same ID, using this code:


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

I passed the ID directly using this code:


'params'=>array(':projectId'=>$id,

…There is something bad with my code? ???

Thanks for the solution. It worked like a charm.

If you still having issue with your display after using this solution code due to some parse error like

Parse error: syntax error, unexpected ‘?’ in C:\xampp\htdocs\trackstar\protected\views\issue\_view.php on line 16

Plese dont blindly copy and paste from book pdf file there is a mistake of ">" in _view.php

In book this is

<b><?php echo CHtml::encode($data->getAttributeLabel(‘status_id’)); ?:</b>

<?php echo CHtml::encode($data->status_id); ?>

alter this with following:-

<b><?php echo CHtml::encode($data->getAttributeLabel(‘status_id’)); ?>:</b>

<?php echo CHtml::encode($data->status_id); ?>

Thank you so much. Solution OP worked for me too.

thanks LeoCS… :D