Page 210 missing argument for loadModel()

Hi everybody,

I am following the book, but I am running into a problem on page 210, when trying to view the url http:://localhost/trackstar/index.php?r=project/view&id=1.

I use Yii version 1.1.10 . Because I am also working on a project with my team, I am unable to use the old version 1.1.2 of Yii.

The initial problem I had was this: I got an error message: “Missing argument 1 for ProjectController::loadModel()” . Which is correct, because the auto-generated method is “public function loadModel($id)”. Here is the code for loadmodel():




public function loadModel($id)

	{

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

		if($model===null)

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

		return $model;

	}



As I understand, it has to do with me using a newer version of Yii, but as I noted before I cannot use the old version.

So I tried to bypass this by adding the id to the loadModel method in actionAddUser() method, so my code would be:




public function actionAddUser() {

            $form = new ProjectUserForm;

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

            // collect user input data

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

		{

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

                        $form->project = $project;

                        // validate user input and set a successfull flash message if valid

			if($form->validate()) {

                            Yii::app()->user->setFlash('success', $form->username ." has been added to the project. ");

                        }

		}

            // display the add user form

            $users = User::model()->findAll();

            $usernames = array();

            foreach($users as $user) {

                $usernames[] = $user->username;

            }

            $form->project = $project;

            $this->render('adduser', array('model'=>$form, 'usernames'=>$usernames));

        }



However, this gives another error message: “include(exists.php): failed to open stream: No such file or directory”. This one does not make any sense to me.

Does anyone know how to make this work?

Here is the complete stacktrace for the last error:




PHP warning


include(exists.php): failed to open stream: No such file or directory


/home/deridder/yii/framework/YiiBase.php(418)


406                 {

407                     foreach(self::$_includePaths as $path)

408                     {

409                         $classFile=$path.DIRECTORY_SEPARATOR.$className.'.php';

410                         if(is_file($classFile))

411                         {

412                             include($classFile);

413                             break;

414                         }

415                     }

416                 }

417                 else

418                     include($className.'.php');

419             }

420             else  // class name with namespace in PHP 5.3

421             {

422                 $namespace=str_replace('\\','.',ltrim($className,'\\'));

423                 if(($path=self::getPathOfAlias($namespace))!==false)

424                     include($path.'.php');

425                 else

426                     return false;

427             }

428             return class_exists($className,false) || interface_exists($className,false);

429         }

430         return true;


Stack Trace

#0 	

+

 /home/deridder/yii/framework/YiiBase.php(418): YiiBase::autoload()

#1 	

+

 /home/deridder/yii/framework/YiiBase.php(298): YiiBase::autoload("exists")

#2 	

+

 /home/deridder/yii/framework/validators/CValidator.php(171): YiiBase::import("exists", true)

#3 	

+

 /home/deridder/yii/framework/base/CModel.php(285): CValidator::createValidator("exists", ProjectUserForm, "username", array("className" => "User"))

#4 	

+

 /home/deridder/yii/framework/base/CModel.php(259): CModel->createValidators()

#5 	

+

 /home/deridder/yii/framework/base/CModel.php(302): CModel->getValidators("username")

#6 	

+

 /home/deridder/yii/framework/web/helpers/CHtml.php(1185): CModel->isAttributeRequired("username")

#7 	

+

 /home/deridder/yii/framework/web/widgets/CActiveForm.php(560): CHtml::activeLabelEx(ProjectUserForm, "username", array())

#8 	

–

 /home/deridder/NetBeansProjects/trackstar/protected/views/project/adduser.php(28): CActiveForm->labelEx(ProjectUserForm, "username")


23 <div class="form">

24     <?php $form = $this->beginWidget('CActiveForm'); ?>

25     <p class="note">Fields with <span class="required">*</span> are required.</p>

26     

27     <div class="row">

28         <?php echo $form->labelEx($model, 'username'); ?>

29         <?php 

30             $this->widget('CAutoComplete', array(

31                 'model'=>$model,

32                 'attribute'=> 'username',

33                 'data'=> $usernames,


#9 	

+

 /home/deridder/yii/framework/web/CBaseController.php(127): require("/home/deridder/NetBeansProjects/trackstar/protected/views/projec...")

#10 	

+

 /home/deridder/yii/framework/web/CBaseController.php(96): CBaseController->renderInternal("/home/deridder/NetBeansProjects/trackstar/protected/views/projec...", array("model" => ProjectUserForm, "usernames" => array("Test_User_One", "Test_User_Two")), true)

#11 	

+

 /home/deridder/yii/framework/web/CController.php(870): CBaseController->renderFile("/home/deridder/NetBeansProjects/trackstar/protected/views/projec...", array("model" => ProjectUserForm, "usernames" => array("Test_User_One", "Test_User_Two")), true)

#12 	

+

 /home/deridder/yii/framework/web/CController.php(783): CController->renderPartial("adduser", array("model" => ProjectUserForm, "usernames" => array("Test_User_One", "Test_User_Two")), true)

#13 	

–

 /home/deridder/NetBeansProjects/trackstar/protected/controllers/ProjectController.php(211): CController->render("adduser", array("model" => ProjectUserForm, "usernames" => array("Test_User_One", "Test_User_Two")))


206             $usernames = array();

207             foreach($users as $user) {

208                 $usernames[] = $user->username;

209             }

210             $form->project = $project;

211             $this->render('adduser', array('model'=>$form, 'usernames'=>$usernames));

212         }

213 }


#14 	

+

 /home/deridder/yii/framework/web/actions/CInlineAction.php(50): ProjectController->actionAddUser()

#15 	

+

 /home/deridder/yii/framework/web/CController.php(309): CInlineAction->runWithParams(array("r" => "project/adduser", "id" => "1"))

#16 	

+

 /home/deridder/yii/framework/web/filters/CFilterChain.php(134): CController->runAction(CInlineAction)

#17 	

+

 /home/deridder/yii/framework/web/filters/CFilter.php(41): CFilterChain->run()

#18 	

+

 /home/deridder/yii/framework/web/CController.php(1146): CFilter->filter(CFilterChain)

#19 	

+

 /home/deridder/yii/framework/web/filters/CInlineFilter.php(59): CController->filterAccessControl(CFilterChain)

#20 	

+

 /home/deridder/yii/framework/web/filters/CFilterChain.php(131): CInlineFilter->filter(CFilterChain)

#21 	

+

 /home/deridder/yii/framework/web/CController.php(292): CFilterChain->run()

#22 	

+

 /home/deridder/yii/framework/web/CController.php(266): CController->runActionWithFilters(CInlineAction, array("accessControl"))

#23 	

+

 /home/deridder/yii/framework/web/CWebApplication.php(276): CController->run("adduser")

#24 	

+

 /home/deridder/yii/framework/web/CWebApplication.php(135): CWebApplication->runController("project/adduser")

#25 	

+

 /home/deridder/yii/framework/base/CApplication.php(162): CWebApplication->processRequest()

#26 	

–

 /home/deridder/NetBeansProjects/trackstar/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();




Thanks, Edwin

I am quite new to Yii myself as of yet and am not as far into the book yet, but here’s my take on your issue:

It looks like you have a typo in your /models/ProjectUserForm.php class, in the rules() method. You put "exists" instead of "exist":

Should be:


array('username', 'exist', 'className'=>'User'),

The framework is try to load a validator of the "exists" type instead of "exist" as it should be, and this is why it is looking for the nonexistent exists.php file in the framework:


CValidator::createValidator("exists", ProjectUserForm, "username", array("className" => "User"))

Thanks CDK, you were right about the typo. Now it works fine.