Commits

Manel Monguilod  committed aff449a

* Changed the behaviour to solve problems related with contains()

  • Participants
  • Parent commits 4337d27

Comments (0)

Files changed (1)

File lib/behavior/TaggableBehavior.php

 /**
  * Add tags
  * @param   array|string    \$tags A string for a single tag or an array of strings for multiple tags
+ * @param   PropelPDO       \$con optional connection object
  */
-public function addTags(\$tags) {
+public function addTags(\$tags, PropelPDO \$con = null) {
     \$arrTags = is_string(\$tags) ? explode(',', \$tags) : \$tags;
         // Remove duplicate tags. 
     \$arrTags = array_intersect_key(\$arrTags, array_unique(array_map('strtolower', \$arrTags)));
     foreach (\$arrTags as \$tag) {
         \$tag = trim(\$tag);
         if (\$tag == \"\") return;
-        \$theTag = {$this->tagTable->getPhpName()}Query::create()->filterByName(\$tag)->findOne();
+        \$theTag = {$this->tagTable->getPhpName()}Query::create()->filterByName(\$tag)->findOne(\$con);
 
         // if the tag do not already exists
         if (null === \$theTag) {
             // create the tag
             \$theTag = new {$this->tagTable->getPhpName()}();
             \$theTag->setName(\$tag);
-            \$theTag->save();
+            \$theTag->save(\$con);
         }
-
-        if (!\$this->getTags()->contains(\$theTag))
+          // Add the tag **only** if not already associated 
+        \$found = false;
+        \$coll = \$this->getTags(null, \$con);
+        foreach (\$coll as \$t) {
+            if (\$t->getId() == \$theTag->getId()) {
+                \$found = true;
+                break;  
+            }
+        }
+        if (!\$found) {
             \$this->addTag(\$theTag);
+        }
     }
 }