教义:如何操作集合?

发布于 2024-10-10 21:52:03 字数 780 浏览 1 评论 0原文

与 symfony &&原则 1.2 在一个操作中,我尝试为用户显示排名最高的网站。

我做到了:

 public function executeShow(sfWebRequest $request)
  {
    $this->user = $this->getRoute()->getObject();
    $this->websites = $this->user->Websites; 
  }

唯一的问题是它返回一个 Doctrine 集合,其中包含所有网站,而不仅仅是排名靠前的网站。

我已经设置了一个方法(getTopRanked()),但如果我这样做:

$this->user->Websites->getTopRanked()

它会失败。

如果有人有想法改变 Doctrine 集合以仅过滤排名最高的。

谢谢

PS:我的方法看起来像(在 websiteTable.class.php 中):

   public function getTopRanked()
{
  $q = Doctrine_Query::create()
       ->from('Website')
      ->orderBy('nb_votes DESC')
       ->limit(5);
  return $q->execute();

}

With symfony && doctrine 1.2 in an action, i try to display the top ranked website for a user.

I did :

 public function executeShow(sfWebRequest $request)
  {
    $this->user = $this->getRoute()->getObject();
    $this->websites = $this->user->Websites; 
  }

The only problem is that it returns a Doctrine collection with all the websites in it and not only the Top ranked ones.

I already setup a method (getTopRanked()) but if I do :

$this->user->Websites->getTopRanked()

It fails.

If anyone has an idea to alter the Doctrine collection to filter only the top ranked.

Thanks

PS: my method looks like (in websiteTable.class.php) :

   public function getTopRanked()
{
  $q = Doctrine_Query::create()
       ->from('Website')
      ->orderBy('nb_votes DESC')
       ->limit(5);
  return $q->execute();

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

被翻牌 2024-10-17 21:52:03

我宁愿在方法之间传递 Doctrine_Query:

//action
public function executeShow(sfWebRequest $request)   
{
   $this->user = $this->getRoute()->getObject();
   $this->websites = $this->getUser()->getWebsites(true);  
}

//user
  public function getWebsites($top_ranked = false)
  {
    $q = Doctrine_Query::create()
     ->from('Website w')
     ->where('w.user_id = ?', $this->getId());
    if ($top_ranked)
    {
      $q = Doctrine::getTable('Website')->addTopRankedQuery($q);
    }
    return $q->execute();
  }

//WebsiteTable
public function addTopRankedQuery(Doctrine_Query $q)
{
  $alias = $q->getRootAlias();
  $q->orderBy($alias'.nb_votes DESC')
    ->limit(5)
  return $q
}

I'd rather pass Doctrine_Query between methods:

//action
public function executeShow(sfWebRequest $request)   
{
   $this->user = $this->getRoute()->getObject();
   $this->websites = $this->getUser()->getWebsites(true);  
}

//user
  public function getWebsites($top_ranked = false)
  {
    $q = Doctrine_Query::create()
     ->from('Website w')
     ->where('w.user_id = ?', $this->getId());
    if ($top_ranked)
    {
      $q = Doctrine::getTable('Website')->addTopRankedQuery($q);
    }
    return $q->execute();
  }

//WebsiteTable
public function addTopRankedQuery(Doctrine_Query $q)
{
  $alias = $q->getRootAlias();
  $q->orderBy($alias'.nb_votes DESC')
    ->limit(5)
  return $q
}
雨轻弹 2024-10-17 21:52:03

如果 getTopRanked() 是您的用户模型中的方法,那么您可以使用 $this->user->getTopRanked() 访问它

If getTopRanked() is a method in your user model, then you would access it with $this->user->getTopRanked()

执着的年纪 2024-10-17 21:52:03

在您的情况下 $this->user->Websites 包含所有用户网站。据我所知,没有办法过滤现有的学说集合(除非您将迭代它并选择有趣的元素)。

我只需在 User 类中实现 getTopRankedWebsites() 方法:

class User extends BaseUser
{
  public function getTopRankedWebsites()
  {
    WebsiteTable::getTopRankedByUserId($this->getId());
  }
}

并在 WebsiteTable 中添加适当的查询:

class WebsiteTable extends Doctrine_Table
{
  public function getTopRankedByUserId($userId)
  {
    return Doctrine_Query::create()
     ->from('Website w')
     ->where('w.user_id = ?', array($userId))
     ->orderBy('w.nb_votes DESC')
     ->limit(5)
     ->execute();
  }
}

In your case $this->user->Websites contains ALL user websites. As far as I know there's no way to filter existing doctrine collection (unless you will iterate through it and choose interesting elements).

I'd simply implement getTopRankedWebsites() method in the User class:

class User extends BaseUser
{
  public function getTopRankedWebsites()
  {
    WebsiteTable::getTopRankedByUserId($this->getId());
  }
}

And add appropriate query in the WebsiteTable:

class WebsiteTable extends Doctrine_Table
{
  public function getTopRankedByUserId($userId)
  {
    return Doctrine_Query::create()
     ->from('Website w')
     ->where('w.user_id = ?', array($userId))
     ->orderBy('w.nb_votes DESC')
     ->limit(5)
     ->execute();
  }
}
晚雾 2024-10-17 21:52:03

您还可以使用 getFirst() 函数

$this->user->Websites->getTopRanked()->getFirst()

http://www.doctrine-project.org/api/orm/1.2/doctrine/doctrine_collection.html#getFirst()

You can also use the getFirst() function

$this->user->Websites->getTopRanked()->getFirst()

http://www.doctrine-project.org/api/orm/1.2/doctrine/doctrine_collection.html#getFirst()

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文