Zabs
Zabs

Reputation: 14142

Trying to get property of non object in Yii model

I have the following controller logic which returns a 'non object error'

This happens once I add the user second line.... any ideas?

$parentuser = new MyparentsMyuser::model()->findAll();
$parentuser->setAttributes(array(
    'user_id' => Yii::app()->controller->user->id,
    'parent_id' => 4259
));

Upvotes: 1

Views: 5267

Answers (2)

crafter
crafter

Reputation: 6297

You are missing one of the golden rules of any development : check your result.

The first line in your code is expected to return either an array of models or an empty array.

// findAll() Returns an array of results.
// Wrong !! You are combining new and findall
// $parentuser = new MyparentsMyuser::model()->findAll();
$parentuser = MyparentsMyuser::model()->findAll();

Then, considering my comments about the expected return value, you usage on line 2 is invalid.

// Wrong!! You cannot apply the setAttributes() method to an array.
$parentuser->setAttributes( ...);

You therefore have to parse the array and apply the setAttributes to each element, if this is what you intended.

Upvotes: 1

itnelo
itnelo

Reputation: 1103

I think you got this error because there are some typing mistake.

$parentuser = new MyparentsMyuser::model()->findAll();

replace to

$parentuser = MyparentsMyuser::model()->findAll();

Upvotes: 2

Related Questions