From f501cfd798e58b3e8e0371918933945673e612ee Mon Sep 17 00:00:00 2001 From: Alexander Sazanovich Date: Fri, 15 Mar 2013 10:18:13 +0300 Subject: [PATCH 1/2] fixes #12 Crashes when owner model has complex PK ( ex.: array('field_1', 'field_2') ) fixes #12 Crashes when owner model has complex PK ( ex.: array('field_1', 'field_2') ) --- ETaggableBehavior.php | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/ETaggableBehavior.php b/ETaggableBehavior.php index 2907abe..fab7e48 100644 --- a/ETaggableBehavior.php +++ b/ETaggableBehavior.php @@ -41,6 +41,11 @@ class ETaggableBehavior extends CActiveRecordBehavior { * @var string|null tag table count field. If null don't uses database. */ public $tagTableCount; + /** + * Owner table PK + * @var string + */ + public $ownerPk; /** * @var string binding table model FK field name. * Defaults to `{model table name with first lowercased letter}Id`. @@ -102,6 +107,9 @@ public function attach($owner) { // If not set cache component, use dummy cache. $this->cache = new CDummyCache; } + + if (null === $this->ownerPk) + $this->ownerPk = $this->ownerPk; parent::attach($owner); } @@ -242,7 +250,7 @@ public function getTagsWithModelsCount($criteria = null) { 'join' => "INNER JOIN {$this->getTagBindingTableName()} et on t.{$this->tagTablePk} = et.{$this->tagBindingTableTagId} ", 'condition' => "et.{$this->getModelTableFkName()} = :ownerid ", 'params' => array( - ':ownerid' => $this->getOwner()->primaryKey, + ':ownerid' => $this->ownerPk, ) )); } else{ @@ -252,7 +260,7 @@ public function getTagsWithModelsCount($criteria = null) { 'condition' => "et.{$this->getModelTableFkName()} = :ownerid ", 'group' => 't.'.$this->tagTablePk, 'params' => array( - ':ownerid' => $this->getOwner()->primaryKey, + ':ownerid' => $this->ownerPk, ) )); } @@ -391,7 +399,7 @@ public function afterSave($event) { $builder->createInsertCommand( $this->getTagBindingTableName(), array( - $this->getModelTableFkName() => $this->getOwner()->primaryKey, + $this->getModelTableFkName() => $this->ownerPk, $this->tagBindingTableTagId => $tagId ) )->execute(); @@ -450,7 +458,7 @@ protected function loadTags($criteria = null) { 'join' => "INNER JOIN {$this->getTagBindingTableName()} et ON t.{$this->tagTablePk} = et.{$this->tagBindingTableTagId} ", 'condition' => "et.{$this->getModelTableFkName()} = :ownerid ", 'params' => array( - ':ownerid' => $this->getOwner()->primaryKey, + ':ownerid' => $this->ownerPk, ) )); if($criteria){ @@ -474,7 +482,7 @@ protected function loadTags($criteria = null) { * @return string */ private function getCacheKey() { - return $this->getCacheKeyBase().$this->getOwner()->primaryKey; + return $this->getCacheKeyBase().$this->ownerPk; } /** * Returns cache key base. @@ -648,7 +656,7 @@ protected function deleteTags() { WHERE %s = %d", $this->getTagBindingTableName(), $this->getModelTableFkName(), - $this->getOwner()->primaryKey + $this->ownerPk ) )->execute(); } @@ -694,7 +702,7 @@ protected function updateCount($count) { $this->tagBindingTableTagId, $this->getTagBindingTableName(), $this->getModelTableFkName(), - $this->getOwner()->primaryKey + $this->ownerPk ) )->execute(); } From aaa111e467c10dd71386831486006c9d36cc65be Mon Sep 17 00:00:00 2001 From: Alexander Sazanovich Date: Fri, 15 Mar 2013 16:29:00 +0300 Subject: [PATCH 2/2] Fix tag saving with new ownerPk --- ETaggableBehavior.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/ETaggableBehavior.php b/ETaggableBehavior.php index fab7e48..b6da167 100644 --- a/ETaggableBehavior.php +++ b/ETaggableBehavior.php @@ -107,7 +107,7 @@ public function attach($owner) { // If not set cache component, use dummy cache. $this->cache = new CDummyCache; } - + if (null === $this->ownerPk) $this->ownerPk = $this->ownerPk; @@ -334,12 +334,12 @@ public function afterSave($event) { 'select' => "t.".$this->tagTablePk, 'params' => array(':tag' => $tag), )); - + if (! $this->tagTableCondition instanceof CDbExpression) $findCriteria->addCondition("t.{$this->tagTableName} = :tag "); else $findCriteria->addCondition($this->tagTableCondition->__toString()); - + if($this->getScopeCriteria()){ $findCriteria->mergeWith($this->getScopeCriteria()); } @@ -370,12 +370,12 @@ public function afterSave($event) { 'select' => "t.".$this->tagTablePk, 'params' => array(':tag' => $tag), )); - + if (! $this->tagTableCondition instanceof CDbExpression) $findCriteria->addCondition("t.{$this->tagTableName} = :tag "); else $findCriteria->addCondition($this->tagTableCondition->__toString()); - + if($this->getScopeCriteria()){ $findCriteria->mergeWith($this->getScopeCriteria()); } @@ -399,7 +399,7 @@ public function afterSave($event) { $builder->createInsertCommand( $this->getTagBindingTableName(), array( - $this->getModelTableFkName() => $this->ownerPk, + $this->getModelTableFkName() => $this->owner->{$this->ownerPk}, $this->tagBindingTableTagId => $tagId ) )->execute();