How to get record with same fixture in Yii unit test

116 Views Asked by At

Its a parent child relation, In childGroup1, getting error during accessing 'PARENT_ID' attribute. The given error is Trying to get property of non-object.

I am having access dynamically.

How to get PARENT_ID in such case.

 return array(
    'group1'=>array(
        'ID' => 1,
        'NAME' => 'Test',
        'STATUS' => 1,
    ),

    'childGroup1'=>array(
        'ID' => 2,
        'PARENT_ID' => $this->getRecord('groups','group1')->ID,
        'NAME' => 'Child Test group1',
        'STATUS' => 1,
    ),
 );
1

There are 1 best solutions below

0
On BEST ANSWER

Since the records are not loaded yet, you cannot use $this->getRecord() to acquire a record. As such, just use plain old array logic to get the record's ID.

$records = array();
$records['group1'] = array(
    'ID' => 1,
    'NAME' => 'Test',
    'STATUS' => 1,
);

$records['childGroup1'] = array(
    'ID' => 2,
    'PARENT_ID' => $records['group1']['ID'],
    'NAME' => 'Child Test group1',
    'STATUS' => 1,
);
return $records;

If you need records from other fixtures, just require them.

$groups = require __DIR__.'/group.php';

This, of course, would be what you put at the top of files OTHER than groups.php, in order to gain access to the groups models.