Hi,
I have an object which I have applied some scopes to like this …
User::model()->notDeleted();
Which is interfering with another object later on in the code, the code for the other object is like this …
User::model()->findByPK($id)->id;
Or similar.
Why is the first object interfering with the second, I thought they both entirely separate objects?
It is not finding the record because its applying the scope but I thought they where seperate?
konapaz
(Konapaz)
June 8, 2013, 12:16pm
2
Hi,
What ecactly you want to do?
Do you want to find no deleted record with specific id ?
No I am really trying to do anything.
I am asking why are the two pieces not treated as separate objects?
The notDeleted() scope is being applied to the findByPK() but I thought they where two separate objects?
konapaz
(Konapaz)
June 8, 2013, 1:05pm
4
JamesBarnsley:
No I am really trying to do anything.
I am asking why are the two pieces not treated as separate objects?
The notDeleted() scope is being applied to the findByPK() but I thought they where two separate objects?
No, the below lines of code should be seperated tasks
User::model()->notDeleted(); //returns filtered "nodeleted" records
User::model()->findByPK($id)->id; //returns specific id independed of "nodeleted" scope.
If the above rules not work properly as they should be, please post all scopes of your model
KonApaz:
No, the below lines of code should be seperated tasks
User::model()->notDeleted(); //returns filtered "nodeleted" records
User::model()->findByPK($id)->id; //returns specific id independed of "nodeleted" scope.
If the above rules not work properly as they should be, please post all scopes of your model
Ok will take a look, might be something else that is causing it. Might be another reason why its not finding the record.
konapaz
(Konapaz)
June 8, 2013, 1:46pm
6
Did you set the notDeleted() scope as default scope? In this case the
User::model()->findByPK($id)->id;
returns the record with specific id (if its a "Nodeleted" case) else returns null
softark
(Softark)
June 8, 2013, 1:52pm
7
The reference says about CActiveRecord::model() …
http://www.yiiframework.com/doc/api/1.1/CActiveRecord#model-detail
So, there are no 2 objects, but only one. The following 2 are the same.
// A
User::model()->notDeleted();
User::model()->findByPK($id);
// B
User::model()->notDeleted()->findByPK($id);
You have to call CActiveRecord::resetScope() to reset the scopes.
http://www.yiiframework.com/doc/api/1.1/CActiveRecord#resetScope-detail
Thanks, I never knew that PHP allowed that. I thought the model has to be returned to variable to do this one …
$user = User::model()->notDeleted();
$user->findByPK($id);
Did not realize that the other way was possible, but thanks for clarifying that.
Will try to look at that in the PHP documentation as well.
Could open up a new paradigm for me.
Does that also apply to things like this?
User::model()->title = "Hello World";
User::model()->save();
softark
(Softark)
June 8, 2013, 2:55pm
9
I think that CActiveRecord::model() is a bit misleading in its naming. Almost always we can think of it as CActiveRecord::getTheFinder() that returns the AR finder static object.
$activeUsers = User::model()->notDeleted()->findAll();
// is equal to the following
// get the finder object for User model
$userFinder = User::model();
// apply 'notDeleted' scope to the finder
$userFinder->notDeleted();
// find all 'not deleted' users
$activeUsers = $userFinder->findAll();
Oh, I never tried that. I don’t know, but probably we’ll get an exception.
[P.S.]
I’m not sure, but you might have misunderstood the return value of the scope method. (Maybe also KonApaz … sorry if I’m wrong, two of you.)
It doesn’t return the filtered result of AR objects. The scope method modifies the finder object by adding a scope to it, and returns the reference to the finder object. That’s why we can chain a method to it. The added scopes are merged with the criteria which you have explicitly defined for find* method when you execute the query.
softark:
I think that CActiveRecord::model() is a bit misleading in its naming. Almost always we can think of it as CActiveRecord::getTheFinder() that returns the AR finder static object.
$activeUsers = User::model()->notDeleted()->findAll();
// is equal to the following
// get the finder object for User model
$userFinder = User::model();
// apply 'notDeleted' scope to the finder
$userFinder->notDeleted();
// find all 'not deleted' users
$activeUsers = $userFinder->findAll();
Oh, I never tried that. I don’t know, but probably we’ll get an exception.
[P.S.]
I’m not sure, but you might have misunderstood the return value of the scope method. (Maybe also KonApaz … sorry if I’m wrong, two of you.)
It doesn’t return the filtered result of AR objects. The scope method modifies the finder object by adding a scope to it, and returns the reference to the finder object. That’s why we can chain a method to it. The added scopes are merged with the criteria which you have explicitly defined for find* method when you execute the query.
Yes, but what I was saying was I can understand this …
User::model()->notDeleted()->findByPK($id)->id;
Or this …
$user = User::model()->notDeleted();
$user->findByPK($id)->id;
I can understand those applying the scopes to the object because its the same object. But I cannot this …
User::model()->notDeleted();
User::model()->findByPK($id)->id;
Because in my mind they where both different objects.