From c2ac52a2f4b2392184c83316269e17c9a6a7bde4 Mon Sep 17 00:00:00 2001 From: Jorge Robles Date: Fri, 4 May 2012 16:58:35 +0200 Subject: [PATCH 1/3] Ticket #8 from http://yiiext.github.com/activerecord-relation-behavior/ Implement scopes to ignore/set the relations to be saved --- .gitignore | 2 ++ EActiveRecordRelationBehavior.php | 55 ++++++++++++++++++++++++++++++- 2 files changed, 56 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 996fc53..5522b78 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ tmp/* yii + +ar.komodoproject diff --git a/EActiveRecordRelationBehavior.php b/EActiveRecordRelationBehavior.php index f071686..0df357e 100644 --- a/EActiveRecordRelationBehavior.php +++ b/EActiveRecordRelationBehavior.php @@ -38,6 +38,11 @@ class EActiveRecordRelationBehavior extends CActiveRecordBehavior public $useTransaction=true; /** @var CDbTransaction */ private $_transaction; + + /** + * Holds relations to be used + */ + protected $_enabledRelations=null; /** * Declares events and the corresponding event handler methods. @@ -118,7 +123,7 @@ public function afterSave($event) /** @var CDbCommandBuilder $commandBuilder */ $commandBuilder=$this->owner->dbConnection->commandBuilder; - foreach($this->owner->relations() as $name => $relation) + foreach($this->getRelations() as $name => $relation) { switch($relation[0]) // relation type such as BELONGS_TO, HAS_ONE, HAS_MANY, MANY_MANY { @@ -356,6 +361,54 @@ protected function parseManyManyFk($name, $relation) return array($joinTable, $fks); } + + + /** + * Scope: Resets the enabled relations to default (Owner's) + * @return CActiveRecord $owner + */ + public function resetRelations() + { + $this->_enabledRelations=null; + return $this->owner; + } + + /** + * Scope: Sets the enabled relations to be used + * @param String relation names + * @param String ... + * + * @return CActiveRecord $owner + */ + public function withRelations() + { + $this->_enabledRelations=func_get_args(); + return $this->owner; + } + + /** + * Scope: Exclude relations from owner's to be used + * @param String relation name + * @param String ... + * + * @return CActiveRecord $owner + */ + public function withoutRelations() + { + $this->_enabledRelations=array_diff(array_keys($this->owner->relations()),func_get_args()); + return $this->owner; + } + + /** + * Return the enabled relations + * @return Array filtered owner relations + */ + protected function getRelations() + { + if (is_null($this->_enabledRelations)) return $this->owner->relations(); + + return array_intersect_key($this->owner->relations(), array_flip($this->_enabledRelations)); + } } /** From 5c6d2ce20375a2da41539290ed2c830ebcfce237 Mon Sep 17 00:00:00 2001 From: Jorge Robles Date: Fri, 4 May 2012 17:14:31 +0200 Subject: [PATCH 2/3] Updated README.md --- README.md | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 5b7e3e7..fe641c9 100644 --- a/README.md +++ b/README.md @@ -42,6 +42,8 @@ public function behaviors() ## Let the magic begin... We have two ActiveRecord classes (the ones from [Yii definitive guide](http://www.yiiframework.com/doc/guide/1.1/en/database.arr#declaring-relationship)): + + ```php profile = new Profile(); $user->profile->save(); // need this to ensure profile got a primary key $user->save(); -``` +## You can also… + +Enable/Disable/Reset the relations to save. To do this you can use: + +`$model->withoutRelations('relation name 1', 'relation name 2',…)->save()` + +…will save all `$model` relations except the ones passed to the scope. + +`$model->withRelations('relation name 1', 'relation name 2',…)->save()` + +…will save only the `$model` relations passed to the scope + +`$model->resetRelations()->save()` + +…will clear the former scopes, and save all the relations of `$model` + ## Some things you should care about... * once you use this behavior you can not set relations by setting the foreign key attributes anymore. From 9f889fde4b19f7d7c3d2a12483eae86c5accdc89 Mon Sep 17 00:00:00 2001 From: Jorge Robles Date: Fri, 4 May 2012 17:16:33 +0200 Subject: [PATCH 3/3] hmm Moe mess --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index fe641c9..81be63c 100644 --- a/README.md +++ b/README.md @@ -69,7 +69,7 @@ class User extends CActiveRecord ); } } - +``` Somewhere in our application code we can do: ```php @@ -103,7 +103,7 @@ Somewhere in our application code we can do: $user->profile = new Profile(); $user->profile->save(); // need this to ensure profile got a primary key $user->save(); - +``` ## You can also…