Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixes #12 #14

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 19 additions & 11 deletions ETaggableBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down Expand Up @@ -103,6 +108,9 @@ public function attach($owner) {
$this->cache = new CDummyCache;
}

if (null === $this->ownerPk)
$this->ownerPk = $this->ownerPk;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is this used for?
You are introducing a new property but never setting it?


parent::attach($owner);
}
/**
Expand Down Expand Up @@ -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{
Expand All @@ -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,
)
));
}
Expand Down Expand Up @@ -326,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());
}
Expand Down Expand Up @@ -362,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());
}
Expand All @@ -391,7 +399,7 @@ public function afterSave($event) {
$builder->createInsertCommand(
$this->getTagBindingTableName(),
array(
$this->getModelTableFkName() => $this->getOwner()->primaryKey,
$this->getModelTableFkName() => $this->owner->{$this->ownerPk},
$this->tagBindingTableTagId => $tagId
)
)->execute();
Expand Down Expand Up @@ -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){
Expand All @@ -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.
Expand Down Expand Up @@ -648,7 +656,7 @@ protected function deleteTags() {
WHERE %s = %d",
$this->getTagBindingTableName(),
$this->getModelTableFkName(),
$this->getOwner()->primaryKey
$this->ownerPk
)
)->execute();
}
Expand Down Expand Up @@ -694,7 +702,7 @@ protected function updateCount($count) {
$this->tagBindingTableTagId,
$this->getTagBindingTableName(),
$this->getModelTableFkName(),
$this->getOwner()->primaryKey
$this->ownerPk
)
)->execute();
}
Expand Down