Help solve the problem Chapter 6


D:\xampp\vhosts\yiidemo.local\www\protected\tests>phpunit unit\ProjectTest.php

PHPUnit 3.5.6 by Sebastian Bergmann.


....E


Time: 13 seconds, Memory: 8.50Mb


There was 1 error:


1) ProjectTest::testGetUserOptions

CException: Property "Project.users" is not defined.


D:\xampp\vhosts\yii\framework\base\CComponent.php:131

D:\xampp\vhosts\yii\framework\db\ar\CActiveRecord.php:110

D:\xampp\vhosts\yiidemo.local\www\protected\models\Project.php:107

D:\xampp\vhosts\yii\framework\base\CComponent.php:112

D:\xampp\vhosts\yii\framework\db\ar\CActiveRecord.php:110

D:\xampp\vhosts\yiidemo.local\www\protected\tests\unit\ProjectTest.php:66


FAILURES!

Tests: 5, Assertions: 10, Errors: 1.


public function getUserOptions()

        {

            $usersArray = CHtml::listData($this->users, 'id', 'username');

            return $usersArray;

        }


public function testGetUserOptions()

    {

        $project = $this->projects('project1');

        $options = $project->userOptions;

        $this->assertTrue(is_array($options));

        $this->assertTrue(count($options)>0);

    }


public $fixtures = array(        

        'projects' => 'Project',

        'users'=>'User',

        'projUsrAssign'=>':tbl_project_user_assignment',

    );



I do not understand what the problem is but I think that is fixture.Maybe I’m wrong. Help please.

Could you please provide the contents of the relations() method within your Project AR class? There should be a relation named "users" that defines the many-to-many relationship between projects and users.




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(

                    'issues' => array(self::HAS_MANY, 'Issue', 'project_id'),

                    'user' => array(self::MANY_MANY, 'User', 'tbl_project_user_assignment(project_id, user_id)'),

		);

	}

There is relations() method my Project AR class.

If I understand the problem correctly, the Project::relations() function has a typo. Fix by changing ‘user’ to ‘users’, i.e.:





public function relations()     {

   return array(

                'issues' => array(self::HAS_MANY, 'Issue', 'project_id'),

                'users' => array(self::MANY_MANY, 'User', 

                           'tbl_project_user_assignment(project_id, user_id)'),

                );

}




I have changed the relations() and the test is pass but there was another error. If i go to yiihostname/index.php?r=issue/create&pid=1 I have this error in line 42


  

PHP Error

Description


Object of class CActiveForm could not be converted to string

Source File


D:\xampp\vhosts\yiidemo.local\www\protected\views\issue\_form.php(42)

00030:         <?php echo $form->dropDOwnList($model,'type_id', $model->getTypeOptions()); ?>

00031:         <?php echo $form->error($model,'type_id'); ?>

00032:     </div>

00033: 

00034:     <div class="row">

00035:         <?php echo $form->labelEx($model,'status_id'); ?>

00036:         <?php echo $form->dropDOwnList($model,'status_id', $model->getStatusOptions()); ?>

00037:         <?php echo $form->error($model,'status_id'); ?>

00038:     </div>

00039: 

00040:     <div class="row">

00041:         <?php echo $form->labelEx($model,'owner_id'); ?>

00042:         <?php echo $form->$form->dropDownList($model,'owner_id', $this->getProject()->getUserOptions()); ?>

00043:         <?php echo $form->error($model,'owner_id'); ?>

00044:     </div>

00045: 

00046:     <div class="row">

00047:         <?php echo $form->labelEx($model,'requester_id'); ?>

00048:         <?php echo $form->$form->dropDownList($model,'requester_id', $this->getProject()->getUserOptions()); ?>

00049:         <?php echo $form->error($model,'requester_id'); ?>

00050:     </div>

00051: 

00052:     <div class="row">

00053:         <?php echo $form->labelEx($model,'create_time'); ?>

00054:         <?php echo $form->textField($model,'create_time'); ?>

Stack Trace

#0 D:\xampp\vhosts\yii\framework\web\CBaseController.php(119): require()

#1 D:\xampp\vhosts\yii\framework\web\CBaseController.php(88): IssueController->renderInternal()

#2 D:\xampp\vhosts\yii\framework\web\CController.php(833): IssueController->renderFile()

#3 D:\xampp\vhosts\yiidemo.local\www\protected\views\issue\create.php(15): IssueController->renderPartial()

#4 D:\xampp\vhosts\yii\framework\web\CBaseController.php(119): require()

#5 D:\xampp\vhosts\yii\framework\web\CBaseController.php(88): IssueController->renderInternal()

#6 D:\xampp\vhosts\yii\framework\web\CController.php(833): IssueController->renderFile()

#7 D:\xampp\vhosts\yii\framework\web\CController.php(746): IssueController->renderPartial()

#8 D:\xampp\vhosts\yiidemo.local\www\protected\controllers\IssueController.php(111): IssueController->render()

#9 D:\xampp\vhosts\yii\framework\web\actions\CInlineAction.php(57): IssueController->actionCreate()

#10 D:\xampp\vhosts\yii\framework\web\CController.php(300): CInlineAction->run()

#11 D:\xampp\vhosts\yii\framework\web\filters\CFilterChain.php(133): IssueController->runAction()

#12 D:\xampp\vhosts\yiidemo.local\www\protected\controllers\IssueController.php(38): CFilterChain->run()

#13 D:\xampp\vhosts\yii\framework\web\filters\CInlineFilter.php(59): IssueController->filterProjectContext()

#14 D:\xampp\vhosts\yii\framework\web\filters\CFilterChain.php(130): CInlineFilter->filter()

#15 D:\xampp\vhosts\yii\framework\web\filters\CFilter.php(41): CFilterChain->run()

#16 D:\xampp\vhosts\yii\framework\web\CController.php(1084): CAccessControlFilter->filter()

#17 D:\xampp\vhosts\yii\framework\web\filters\CInlineFilter.php(59): IssueController->filterAccessControl()

#18 D:\xampp\vhosts\yii\framework\web\filters\CFilterChain.php(130): CInlineFilter->filter()

#19 D:\xampp\vhosts\yii\framework\web\CController.php(283): CFilterChain->run()

#20 D:\xampp\vhosts\yii\framework\web\CController.php(257): IssueController->runActionWithFilters()

#21 D:\xampp\vhosts\yii\framework\web\CWebApplication.php(324): IssueController->run()

#22 D:\xampp\vhosts\yii\framework\web\CWebApplication.php(121): CWebApplication->runController()

#23 D:\xampp\vhosts\yii\framework\base\CApplication.php(135): CWebApplication->processRequest()

#24 D:\xampp\vhosts\yiidemo.local\www\index.php(13): CWebApplication->run()

Change line 42 from:




$form->$form->dropDownList($model,'owner_id', $this->getProject()->getUserOptions()); 



to




$form->dropDownList($model,'owner_id', $this->getProject()->getUserOptions()); 



By the way, instead of giving all of the output from your code, it would be helpful for you to isolate just the parts that aren’t working. It’s kind of hard to ferret out the problem when everything is just printed this way. IMHO. <_<

Hello friends

I’m from Brazil, sorry if my english is not very good.

I’m Studying the same book, and I have the same error:

My relation is right, but the test don’t pass. I need help to fix this error, see my code:




public function getUserOptions()

        {

            $usersArray = CHtml::listData($this->users, 'id', 'username');

            return $usersArray;


        }

public function testGetUserOptions()

   {

       $project = $this->projects('project1');

        $options = $project->userOptions;

        $this->assertTrue(is_array($options));

        $this->assertTrue(count($options)>0);


   }

 public $fixtures=array

    (

        'projects' => 'Project',

        'users'=>'User',

        'projUsrAssign'=>':tbl_project_user_assignment',


    );


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(

			'issues' => array(self::HAS_MANY, 'Issue', 'project_id'),

			 'users' => array(self::MANY_MANY, 'User', 'tbl_project_user_assignment(project_id, user_id)'),

		);

	}




How can I fix it?

Friends I fix de problem. I put the code on wrong class. I need sleep srsrsrsr

@Alexandre Carmo

plz tell us the solution for same.

I got a solution from Hayden so all Credit Goes to Hayden

Alter your Project:: relations() method with following :-

public function relations() {

    return array(


        'issues' =&gt; array(self::HAS_MANY, 'Issue', 'project_id'),


        'users' =&gt; array(self::MANY_MANY, 'User', 'tbl_project_user_assignment(project_id, user_id)'),


    );


}