Object Interfering With Other Object


I have an object which I have applied some scopes to like this …


Which is interfering with another object later on in the code, the code for the other object is like this …


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?


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?

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.

Did you set the notDeleted() scope as default scope? In this case the


returns the record with specific id (if its a "Nodeleted" case) else returns null

The reference says about CActiveRecord::model() …


So, there are no 2 objects, but only one. The following 2 are the same.

// A



// B


You have to call CActiveRecord::resetScope() to reset the scopes.


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();


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";


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


// find all 'not deleted' users

$activeUsers = $userFinder->findAll();

Oh, I never tried that. I don’t know, but probably we’ll get an exception.


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 …


Or this …

$user = User::model()->notDeleted();


I can understand those applying the scopes to the object because its the same object. But I cannot this …



Because in my mind they where both different objects.