Missing Argument - Chapter 8

I’m getting a “Missing Argument” error for the loadModel() function (see error below) in Chapter 8 when I try to test the addUser method. I thought it might be a result of the difference between my version and the book’s version of Yii, and also saw that there was a similar problem in chapter 6, as described on this thread My link

It seemed like it would be easy to solve because the Missing Argument Error pointed me to the line 194 in ProjectController.php loadModel() method, and, as was done in the thread referred to above, I inserted a $id argument. However, after i put that $id argument in, I got a new error Undefined Variable see further below.

Does anyone have an idea how to fix this problem? Thanks

Missing Argument Error


PHP Error


Missing argument 1 for ProjectController::loadModel(), called in /Applications/MAMP/htdocs/demo/protected/controllers/ProjectController.php on line 194 and defined


/Applications/MAMP/htdocs/demo/protected/controllers/ProjectController.php(171)


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

160 

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

162             'model'=>$model,

163         ));

164     }

165 

166     /**

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

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

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

170      */

171     public function loadModel($id)

172     {

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

174         if($model===null)

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

176         return $model;

177     }

178 

179     /**

180      * Performs the AJAX validation.

181      * @param CModel the model to be validated

182      */

183     protected function performAjaxValidation($model)

Stack Trace

#0	

–  /Applications/MAMP/htdocs/demo/protected/controllers/ProjectController.php(194): ProjectController->loadModel()

189         }

190     }

191     

192 public function actionAdduser()

193     {

194         $project = $this->loadModel();

195         if(!Yii::app()->user->checkAccess('createUser', array('project'=>$project)))

196         {

197             throw new CHttpException(403,'You are not authorized to per-form this action.');

198         } 

199         $form=new ProjectUserForm; 

UNDEFINED VARIABLE ERROR MESSAGE


PHP Error


Undefined variable: id


/Applications/MAMP/htdocs/demo/protected/controllers/ProjectController.php(194)


182      */

183     protected function performAjaxValidation($model)

184     {

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

186         {

187             echo CActiveForm::validate($model);

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

189         }

190     }

191     

192 public function actionAdduser()

193     {

194         $project = $this->loadModel($id);

195         if(!Yii::app()->user->checkAccess('createUser', array('project'=>$project)))

196         {

197             throw new CHttpException(403,'You are not authorized to per-form this action.');

198         } 

199         $form=new ProjectUserForm; 

200         // collect user input data

201         if(isset($_POST['ProjectUserForm']))

202         {

203             $form->attributes=$_POST['ProjectUserForm'];

204             $form->project = $project;

205             // validate user input and set a sucessfull flassh message if valid   

206             if($form->validate())  

Stack Trace

#0	

+  /Applications/MAMP/htdocs/framework/web/actions/CInlineAction.php(50): ProjectController->actionAdduser()

#1	

+  /Applications/MAMP/htdocs/framework/web/CController.php(300): CInlineAction->runWithParams(array())

#2	

+  /Applications/MAMP/htdocs/framework/web/filters/CFilterChain.php(133): CController->runAction(CInlineAction)

#3	

+  /Applications/MAMP/htdocs/framework/web/filters/CFilter.php(41): CFilterChain->run()

#4	

+  /Applications/MAMP/htdocs/framework/web/CController.php(1122): CFilter->filter(CFilterChain)

#5	

+  /Applications/MAMP/htdocs/framework/web/filters/CInlineFilter.php(59): CController->filterAccessControl(CFilterChain)

#6	

+  /Applications/MAMP/htdocs/framework/web/filters/CFilterChain.php(130): CInlineFilter->filter(CFilterChain)

#7	

+  /Applications/MAMP/htdocs/framework/web/CController.php(283): CFilterChain->run()

#8	

+  /Applications/MAMP/htdocs/framework/web/CController.php(257): CController->runActionWithFilters(CInlineAction, array("accessControl"))

#9	

+  /Applications/MAMP/htdocs/framework/web/CWebApplication.php(328): CController->run("adduser")

#10	

+  /Applications/MAMP/htdocs/framework/web/CWebApplication.php(121): CWebApplication->runController("project/adduser")

#11	

+  /Applications/MAMP/htdocs/framework/base/CApplication.php(155): CWebApplication->processRequest()

#12	

–  /Applications/MAMP/htdocs/demo/index.php(13): CApplication->run()

08 defined('YII_DEBUG') or define('YII_DEBUG',true);

09 // specify how many levels of call stack should be shown in each log message

10 defined('YII_TRACE_LEVEL') or define('YII_TRACE_LEVEL',3);

11 

12 require_once($yii);

13 Yii::createWebApplication($config)->run();

Can you show me where you used actionAdduser()?

if you are gonna use


$project = $this->loadModel();

you have to supply something inside loadModel’s parameter.

and if you are getting an error when you added $id into loadModel, it’s because Yii or I mean, PHP, could not figure out what’s the value of $id.

When using $id, you should make sure that there is a value in it, or if actionAdduser is used somewhere else, make sure that you got $id on your parameter and all codes that uses your actionAdduser should supply an integer on it’s parameter.




192 public function actionAdduser($id)

    {

         $project = $this->loadModel($id);     //the value of $id is what ever is passed into actionAdduser's param.



O.K, thank you very much, adding the argument took care of the error message. However, I’m now getting a 400 page when I click to add user. However, that’s another topic, so I will post another topic.