Skip to content

Commit 7cd1d9e

Browse files
committed
check AR->hasRelated($name) for performace
only touch relations that have been assigned or loaded. fixes #20
1 parent 27640f9 commit 7cd1d9e

File tree

1 file changed

+3
-5
lines changed

1 file changed

+3
-5
lines changed

EActiveRecordRelationBehavior.php

+3-5
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@
2222
* - currently handles all existing relations, will add support for limitation shortly
2323
* - relations defined with 'through' are not supported yet (http://www.yiiframework.com/doc/guide/1.1/en/database.arr#relational-query-with-through)
2424
*
25-
* @todo use AR->hasRelated($name)
26-
*
2725
* @property CActiveRecord $owner The owner AR that this behavior is attached to.
2826
*
2927
* @author Carsten Brandt <[email protected]>
@@ -73,7 +71,7 @@ public function beforeValidate($event)
7371
// attribute of $this->owner has to be changed
7472
case CActiveRecord::BELONGS_TO:
7573

76-
if (!$this->isRelationSupported($relation))
74+
if (!$this->owner->hasRelated($name) || !$this->isRelationSupported($relation))
7775
break;
7876

7977
$pk=null;
@@ -130,7 +128,7 @@ public function afterSave($event)
130128
*/
131129
case CActiveRecord::MANY_MANY:
132130

133-
if (!$this->isRelationSupported($relation))
131+
if (!$this->owner->hasRelated($name) || !$this->isRelationSupported($relation))
134132
break;
135133

136134
Yii::trace('updating MANY_MANY table for relation '.get_class($this->owner).'.'.$name,'system.db.ar.CActiveRecord');
@@ -174,7 +172,7 @@ public function afterSave($event)
174172
case CActiveRecord::HAS_MANY:
175173
case CActiveRecord::HAS_ONE:
176174

177-
if (!$this->isRelationSupported($relation))
175+
if (!$this->owner->hasRelated($name) || !$this->isRelationSupported($relation))
178176
break;
179177

180178
Yii::trace(

0 commit comments

Comments
 (0)