On my work many times used defaultScope, which defines not-deleted models and only in garbage it need to disable. Yii (my version 1.1.14) can’t reset defaultScope with a ‘scope’ key in relations. To rewrite all framework models need too many time, so I make a little patch for two classes CAvtiveRecord and CActiveFinder:
diff --git a/framework/db/ar/CActiveFinder.php b/framework/db/ar/CActiveFinder.php
index a93fd3b..62c3a92 100644
--- a/framework/db/ar/CActiveFinder.php
+++ b/framework/db/ar/CActiveFinder.php
@@ -246,7 +246,7 @@ class CActiveFinder extends CComponent
if(!empty($options['scopes']))
$scopes=array_merge($scopes,(array)$options['scopes']); // no need for complex merging
- $model->resetScope(false);
+ $model->resetScope(isset($relation->resetDefaultScope) ? $relation->resetDefaultScope : false);
$criteria=$model->getDbCriteria();
$criteria->scopes=$scopes;
$model->beforeFindInternal();
diff --git a/framework/db/ar/CActiveRecord.php b/framework/db/ar/CActiveRecord.php
index c40c5b8..4e845a6 100644
--- a/framework/db/ar/CActiveRecord.php
+++ b/framework/db/ar/CActiveRecord.php
@@ -2152,6 +2152,10 @@ class CActiveRelation extends CBaseActiveRelation
* @since 1.1.7
*/
public $through;
+ /**
+ * @var boolean whether reset default scope for relations
+ */
+ public $resetDefaultScope;
/**
* Merges this relation with a criteria specified dynamically.
@@ -2195,6 +2199,9 @@ class CActiveRelation extends CBaseActiveRelation
if(isset($criteria['together']))
$this->together=$criteria['together'];
+
+ if(isset($criteria['resetDefaultScope']))
+ $this->resetDefaultScope=$criteria['resetDefaultScope'];
}
}
Patch for crazy people like me - when framework updated it will need to patch again =)
Do you mean you overwrote framework files directly?
That’s very unhandy and a really bad practice.
You can extend framework classes, putting a new class (e.g. YourActiveRecord or whatever), for example, into /components folder (I usually have a specific folder for such framework extended classes inside of /components). Then you may extend your models from a new class (YourActiveRecord). This way you don’t need to hack framework directly and may implement anything you need.
Yeah, I know about it. I was trying to make an extended class with a necessary overloaded methods, but that action pulling many related requirements, like as private variables in the CActiveRecord and CActiveFinder classes (and in C***Relation classes too), so there need to patching a framework classes anyway.
I killed a two day to try a true-way, but a lot of extraneous errors pops up (of course related with inheritance).
As you can see, I patched CActiveRelation class, and if we make extended on it classes *Relation we need to overload CActiveRecord (constants and two methods getActiveFinder and getRelated) and CActiveFinder (buildJoinTree as minimal, and some other to correct a conditions). In result - boom(!), we got troubles with private variables, about I told you early.
Based on this, I don’t know what would be better - rewrite many framework classes or a tiny patch.