编写dql查询时出错

时间:2011-01-28 22:55:13

标签: symfony1 doctrine dql

我正在使用插件将标签添加到我的数据库的内容中,我在编写以下查询时遇到了一些困难:

我想要标记为至少一个已发布内容的标记列表,所以我尝试了:

$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();

但是插件中没有指定标记和内容之间的关系,因此Doctrine会抛出“未知关系别名”异常。

修改

我尝试使用 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));

但它仍然显示所有标签,并且数字为奇数。

2 个答案:

答案 0 :(得分:0)

我认为这是一个很好的做法:

  1. 在他们之间建立关系
  2. 实施CountCache行为
  3. 因此,在这种情况下,您可以轻松使用标记例程。

答案 1 :(得分:0)

知道了!我想过下载Apostrophe插件和搜索 对于 getAllTagNameWithCount 调用

    $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));