Chapter 6 Page 131 - Missing Argument 1 For Projectcontroller::loadmodel()

Hello,

I am new to yii and following the Agile web development book. I have followed all the instructions so far and had no problem until then end of page 131.

I get this warning

Missing argument 1 for ProjectController::loadModel(), called in C:\wamp\www\trackstar\protected\controllers\ProjectController.php [color="#FF0000"]on line 56 and [/color]defined

/**

  • Displays a particular model.

*/

public function actionView()

{

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


  'criteria'=>array(


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


    'params'=>array(':projectId'=>$this->loadModel()->id), //[color="#FF0000"]This is line 56[/color]


  ),


  'pagination'=>array(


    'pageSize'=>1,


  ),


));





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


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


  'issueDataProvider'=>$issueDataProvider,


));

}

loadModel method needs an id to return a model and actionView method needs it too so you can modify your code as follow




public function actionView($id)

{

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

   'criteria'=>array(

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

   'params'=>array(':projectId'=>$this->loadModel($id)->id), //you can pass $id directly (loadModel method is not necessary here!)

  ),

  'pagination'=>array(

  'pageSize'=>1,

 ),

));

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

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

'issueDataProvider'=>$issueDataProvider,

));

}









Thankyou Reza but now I get a 404.

[i]Error 404

The requested page does not exist.[/i]

view is to show database data by using id and your url should look like this (according to book):

localhost/tasctrak/index.php?r=project/view&id=1

so if you try to retrive a data that doesn’t exist in your db, you get 404 error

The URL is correct. I am using the same URL. There are 2 projects and there is 1 issue attached to each project by following the whole procedure in the book. I have re-checked the code many times, it all looks to be correct. I think may be I missed some thing but I cant figure out what.

I think you simply don’t have a project with id 1 in your database, check your db

I have two projects in projects table with IDs 1 and 2.

See this other topic in this forum with resolution of the same issue:

http://www.yiiframework.com/forum/index.php/topic/12990-got-stuck-at-chapter-6/

Appears to be due to a change in the framework between the version the book was written for, and later versions.