loadModel() without parameter, is allowed?

in chapter 5 page 129 there is code:


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

'criteria'=>array(

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

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

),

'pagination'=>array(

'pageSize'=>1,

),

));

its call loadModel() method without parameter and cause a following error:


Error 500


Missing argument 1 for ProjectController::loadModel(), called in F:\wamp\www\trackstar\protected\controllers\ProjectController.php on line 56 and defined

i think the writer was made the mistake or me that was wrong? can anyone tell me what is the problem?

Change actionView method to accept parameter $id




public function actionView($id)



and then modify CActiveDataProvider parameters as follow:




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

    'criteria'=>array(

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

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

    ),

    'pagination'=>array(

        'pageSize'=>1,

    ),

));



This should work.

thank you Ivica. another think that i want to make sure is, why the book wrote ‘$this->loadModel()->$id’ instead of just ‘$id’?

I check code that comes with book, and I think they have different solution:




/**

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

	 */

	public function loadModel()

	{

		if($this->_model===null)

		{

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

				$this->_model=Project::model()->findbyPk($_GET['id']);

			if($this->_model===null)

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

		}

		return $this->_model;

	}



You can try to see if it works.

There are many errors in this book, however, I think it is still very good book to start with Yii framework and TDD.

You right Ivica, i have check the code resource belongs to the writer in github and it turns out the loadModel() has been overwrote. but i not remember the book tell me to do it in the previous pages :D

The reason why the code from the book doesn’t work for you is because the book was written using the older Yii 1.1.2 while you are using a newer version. The implementation of loadModel has changed in versions after version 1.1.2.

thanks! it the reason! :)