trouble writing a dql query
I'm using a plugin to add tags to the contents of my database, and I'm having some difficulties in writing the following query :
I want the list of tags that are tagged to at least one published content, so I tried :
$this->tagsNumber = Doctrine_Core::getTable('Tag')
->createQuery('t')
->select('t.name, count(tg.tag_id) as nbr')
->innerJoin('t.Tagging tg')
->innerJoin('Content c on c.id = tg.taggable_id')
->where('c.state = ?', 3)
->orderBy('nbr DESC, t.name')
->groupBy('tg.tag_id')
->execute();
But the relation between Tagging and Content isn't specified in the plugin, so Doctrine throws a "unkown relation alias" exception.
EDIT
I tried using the functions in PluginTagTable.class.php :
$q = Doctrine_Query::create()
->select('tg.tag_id, t.name, COUNT(tg.id) AS t_count')
->from('Tagging tg, tg.Tag t, Content c on tg.model_id = c.id')
->where('c.state_id = ?', 3);
$this->etiquettesOrdre = PluginTagTable::getAllTagNameWithCount($q,Array('model' => 'Content', 'sort_by_popularity' => true));
开发者_StackOverflow中文版
But it still shows all tags, and weird numbers as the count.
I think it's a good practice to:
- Create a relation between them
- Implement a CountCache behavior
So in this case you can easily work with tagging routines.
Got it ! I thought about downloading Apostrophe plugin and searching for getAllTagNameWithCount calls
$q = Doctrine_Query::create()->from('Tagging tg, tg.Tag t, Content
c');
$q->andWhere('c.id = tg.taggable_id and c.state_id = ?', 3);
$this->tagsInOrder =
PluginTagTable::getAllTagNameWithCount($q,Array('model' => 'Content',
'sort_by_popularity' => true));
精彩评论