教义:如何操作集合?
与 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我宁愿在方法之间传递 Doctrine_Query:
I'd rather pass Doctrine_Query between methods:
如果 getTopRanked() 是您的用户模型中的方法,那么您可以使用
$this->user->getTopRanked()
访问它If getTopRanked() is a method in your user model, then you would access it with
$this->user->getTopRanked()
在您的情况下 $this->user->Websites 包含所有用户网站。据我所知,没有办法过滤现有的学说集合(除非您将迭代它并选择有趣的元素)。
我只需在 User 类中实现 getTopRankedWebsites() 方法:
并在 WebsiteTable 中添加适当的查询:
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:
And add appropriate query in the WebsiteTable:
您还可以使用
getFirst()
函数http://www.doctrine-project.org/api/orm/1.2/doctrine/doctrine_collection.html#getFirst()
You can also use the
getFirst()
functionhttp://www.doctrine-project.org/api/orm/1.2/doctrine/doctrine_collection.html#getFirst()