开发者

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:

  1. Create a relation between them
  2. 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)); 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜