0

这是一个完全新手的问题,所以请多多包涵。我正在从在线 Jobeet 和 Askeet 教程中学习 symfony,但我的大部分 hack 都涉及 Doctrine,所以我对 Propel 一点也不熟悉。到目前为止,我已经通过在线研究和修改以满足我的需求来管理,但我需要一些帮助。

有人可以帮我将这些代码片段翻译成 Doctrine 吗?

public function setTag($v)
{
  parent::setTag($v);

  $this->setNormalizedTag(Tag::normalize($v));
}
public function getTags()
{
  $c = new Criteria();
  $c->clearSelectColumns();
  $c->addSelectColumn(QuestionTagPeer::NORMALIZED_TAG);
  $c->add(QuestionTagPeer::QUESTION_ID, $this->getId());
  $c->setDistinct();
  $c->addAscendingOrderByColumn(QuestionTagPeer::NORMALIZED_TAG);

  $tags = array();
  $rs = QuestionTagPeer::doSelectRS($c);
  while ($rs->next())
  {
    $tags[] = $rs->getString(1);
  }

  return $tags;
}
public function getPopularTags($max = 5)
{
  $tags = array();

  $con = Propel::getConnection();
  $query = '
    SELECT %s AS tag, COUNT(%s) AS count
    FROM %s
    WHERE %s = ?
    GROUP BY %s
    ORDER BY count DESC
  ';

  $query = sprintf($query,
    QuestionTagPeer::NORMALIZED_TAG,
    QuestionTagPeer::NORMALIZED_TAG,
    QuestionTagPeer::TABLE_NAME,
    QuestionTagPeer::QUESTION_ID,
    QuestionTagPeer::NORMALIZED_TAG
  );

  $stmt = $con->prepareStatement($query);
  $stmt->setInt(1, $this->getId());
  $stmt->setLimit($max);
  $rs = $stmt->executeQuery();
  while ($rs->next())
  {
    $tags[$rs->getString('tag')] = $rs->getInt('count');
  }

  return $tags;
}
public static function getTagsForUserLike($user_id, $tag, $max = 10)
{
  $tags = array();

  $con = Propel::getConnection();
  $query = '
    SELECT DISTINCT %s AS tag
    FROM %s
    WHERE %s = ? AND %s LIKE ?
    ORDER BY %s
  ';

  $query = sprintf($query,
    QuestionTagPeer::TAG,
    QuestionTagPeer::TABLE_NAME,
    QuestionTagPeer::USER_ID,
    QuestionTagPeer::TAG,
    QuestionTagPeer::TAG
  );

  $stmt = $con->prepareStatement($query);
  $stmt->setInt(1, $user_id);
  $stmt->setString(2, $tag.'%');
  $stmt->setLimit($max);
  $rs = $stmt->executeQuery();
  while ($rs->next())
  {
    $tags[] = $rs->getString('tag');
  }

  return $tags;
}
4

1 回答 1

0

我建议你忘记 Askeet 教程,它是针对 1.0 版本的,自从这个版本以来,很多事情都发生了变化。

但是,您可以找到Askeet 教义版本的svn 转储。您应该使用 SVN 来重建 repo(不知道如何执行)。

另一方面,如果您需要处理 sf1.4 项目中的标签,我建议您使用插件sfDoctrineActAsTaggablePlugin

于 2012-05-15T08:59:13.750 回答