Missing Argument Error CActiveDataProvider

Hello. I’m getting an error in my ProjectsController:

Missing argument 1 for ProjectsController::loadModel(), called in /Applications/XAMPP/xamppfiles/htdocs/quicksand/protected/controllers/ProjectsController.php on line 56 and defined

This is the function in my Project Controller:

public function actionView($id)


{


	$projectitemDataProvider=new CActiveDataProvider('Projectitem', array(


		'criteria'=>array(


	 		'condition'=>'project=:projectId',


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


	 	),


	 	'pagination'=>array(


	 		'pageSize'=>1,


	 	),


	 ));


	


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


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


	));


}

Not sure what the issue is. In the condition: ‘condition’=>‘project=:projectId’, project refers to the foreign key in the “Projectitem model”, as I assume that’s what should be referenced. In the Projectitem model I have the following relation:

public function relations()


{


	// NOTE: you may need to adjust the relation name and the related


	// class name for the relations automatically generated below.


	return array(


	'project' => array(self::BELONGS_TO, 'Projects', 'project'),


	);


}

I assume this relation needs to be defined for the condition to work in the CActiveDataProvider.

The end goal is I want to make the Projectitem a sub record of the Project record, with the Projectitem info being displayed in the Project record it relates to. I’m following the example in the book Agile Web Application Development chapter 6 page 129.

Any help would be appreciated.

are you using standard Gii generator or GiiX? there are differences in those two on loadModel method. standard Gii generates loadModel() function without parameters, while GiiX generates loadModel( $modelName, $primaryKey ) function…

@redguy

Standard gii generates loadModel($id) …

@studentofyii

Just as the error message says, you have to call your ProjectsController::loadModel() with one parameter. And normally it is the id of the Project.




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



And




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

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

));



I am not sure what is your problem. But I’m sure this will definitely help you.

Remember, this code contains lots of codes that will be needed in later chapters. so just ignore them now.

2795

ProjectController.php

wow… you are right. I have not used default generator since about half year, so the change must be rather new :)

anyway - blog demo still has the old version which depends on GET param:




public function loadModel()

	{

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

		{

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

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

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

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

		}

		return $this->_model;

	} 



Thank you that worked.