seyyed
(Arash)
March 30, 2014, 9:48am
1
Guys,
The find() method also supports the following shortcut usage which allows you to retrieve an Active Record instance based on a primary key value or a set of column values. The main difference here is that instead of returning a [[yii\db\ActiveQuery]] instance, the method takes the column value(s) and returns an Active Record instance directly without the need to call one()
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();
seyyed
(Arash)
March 30, 2014, 10:02am
3
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();
seyyed
(Arash)
March 30, 2014, 11:10am
5
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)
bettor
(Live Webscore)
March 30, 2014, 11:31am
7
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
seyyed
(Arash)
March 30, 2014, 12:01pm
8
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.
seyyed
(Arash)
March 30, 2014, 12:24pm
10
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
qiang
(Qiang Xue)
March 30, 2014, 5:18pm
12
Please double checked the value of $userId. Make sure it is not null.
Seems like it was fixed recently