Activerecord::find() Problem

Guys,

According to above document this code must return an ActiveRecord instance but it return ActiveQuery.




$userModel = User::find($userId);

$userModel->deleteAllGroups();



and get this error


Unknown Method – yii\base\UnknownMethodException


Calling unknown method: yii\db\ActiveQuery::deleteAllGroups()

That’s weird. What’s the value of $userId?

And what happens if you do


$userModel = User::find(['id' => $userId])->one();

$userModel->deleteAllGroups();

Yes, I know. $userId is PK. in this example it’s 2




Calling unknown method: app\models\activeRecords\User::one()



Seems like something’s wrong with your model.

Does it extend ActiveRecord?

something like


class User extends \yii\db\ActiveRecord implements \yii\web\IdentityInterface

And the last one:

$userModel = User::find()->where([‘id’ => $userId])->one();

Yes.


Call to a member function deleteAllGroups() on a non-object

Have you by chance overriden createQuery?

(its signature has changed slightly)

How about enable debug module and just run:


$userModel = User::find($userId);

and look at the queries log. Find the query that was produced by the above code and paste it in something like phpmyadmin and see if this actually returns a result.

Also are you working on the latest version of Yii

Weird.

I just add


 return $this->render('user'); 

at the end of action and now work fine!

No, that cannot be a solution.

The problem is still there, it’s just hiding.

My action didn’t has retrun at the end.

Even worked with just return true; now

Weird. Weird. Weird.

Please double checked the value of $userId. Make sure it is not null.

Seems like it was fixed recently