Commits

matteosister committed 986e91c

filterByTagName in quert object

Comments (0)

Files changed (2)

     $article->getTags() // PropelCollection of Tag object
 
 
+    // Query object
+    $articles = ArticleQuery::create()->filterByTagName('propel')->find();
+    // you could also use the propel generated method. filterByTagName is just a shortcut of
+    $articles = ArticleQuery::create()->useArticleTaggingQuery()->useTagQuery()->filterByName('propel')->endUse()->endUse();
+    // if you have a tag object (for example in a list of article tagged with...) propel has already done the dirty job
+    $tag = TagQuery::create()->findOneByName('symfony');
+    $articles = ArticleQuery::create()->filterByTag($tag)->find();
+    
+
+
 Tag widget!
 -----------
 

lib/behavior/TaggableBehavior.php

         $this->createTaggingTable();
     }
 
-    public function objectMethods($builder)
-    {
-        $this->builder = $builder;
-
-        $script = '';
-
-        $this->addAddTagsMethod($script);
-        $this->addRemoveTagMethod($script);
-
-        return $script;
-    }
-
     protected  function createTagTable()
     {
         $table = $this->getTable();
         $this->taggingTable->addForeignKey($fkObj);
     }
 
+    /**
+     * Adds methods to the object
+     */
+    public function objectMethods($builder)
+    {
+        $this->builder = $builder;
+
+        $script = '';
+
+        $this->addAddTagsMethod($script);
+        $this->addRemoveTagMethod($script);
+
+        return $script;
+    }
+
     private function addAddTagsMethod(&$script)
     {
         $table = $this->getTable();
 ";
     }
 
+    /**
+     * Adds method to the query object
+     */
+    public function queryMethods($builder)
+	{
+		$this->builder = $builder;
+		$script = '';
+        
+        $this->addFilterByTagName($script);
+
+		return $script;
+	}
+
+    protected function addFilterByTagName(&$script)
+	{
+		$script .= "
+/**
+ * Filter the query on the tag name
+ *
+ * @param     string \$tagName A single tag name
+ *
+ * @return    " . $this->builder->getStubQueryBuilder()->getClassname() . " The current query, for fluid interface
+ */
+public function filterByTagName(\$tagName)
+{
+    return \$this->use".$this->taggingTable->getPhpName()."Query()->useTagQuery()->filterByName(\$tagName)->endUse()->endUse();
+}
+";
+	}
+
+
     protected function getTagTableName()
 	{
 		return $this->replaceTokens($this->getParameter('tag_table'));