Yii Framework Forum: Reset Defaultscope In Relation - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Reset Defaultscope In Relation Patch for version 1.1.14 Rate Topic: -----

#1 User is offline   theg4sh 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 3
  • Joined: 26-November 13

Posted 26 November 2013 - 05:02 AM

Hi, all!

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 =)
0

#2 User is offline   yugene 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 492
  • Joined: 08-August 09

Posted 26 November 2013 - 09:54 PM

View Posttheg4sh, on 26 November 2013 - 05:02 AM, said:

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.
0

#3 User is offline   yugene 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 492
  • Joined: 08-August 09

Posted 26 November 2013 - 09:54 PM

And welcome to forum :)
0

#4 User is offline   theg4sh 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 3
  • Joined: 26-November 13

Posted 27 November 2013 - 03:41 AM

View Postyugene, on 26 November 2013 - 09:54 PM, said:

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.

View Postyugene, on 26 November 2013 - 09:54 PM, said:

And welcome to forum :)

Thanks :)
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users