Protrines to Doctrine Code Snippets

时间:2012-05-15 07:24:06

标签: doctrine symfony-1.4 propel

这是一个全新的问题,所以请耐心等待。我正在通过在线Jobeet和Askeet教程学习symfony,但我的大多数黑客都涉及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;
}

1 个答案:

答案 0 :(得分:0)

我建议你忘记Askeet教程,它适用于1.0版本,自从这个版本以来发生了很多变化。

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

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