gpt4 book ai didi

php - 如何在 Symfony2 中使用 Controller 中的 EntityManager 对结果进行排序

转载 作者:行者123 更新时间:2023-12-03 23:01:46 26 4
gpt4 key购买 nike

我有这个代码:

public function indexAction()
{
$em = $this->getDoctrine()->getManager();
$user = $this->getUser();
$entities = $em
->getRepository('BreedrGeckoBundle:Weight')
->findBy(array('user' => $user), array());

return array(
'entities' => $entities,
);
}

很好,获取数据库中当前用户的所有权重。
但是我怎样才能对结果进行排序,使同一 gecko id 的所有权重条目都在一起?
我的表结构如下:

enter image description here

最佳答案

您需要使用 Doctrine's QueryBuilder 查询您的对象.
像这样:

$repository = $this->getDoctrine()
->getRepository('BreedrGeckoBundle:Weight');

$query = $repository->createQueryBuilder('w')
->where('w.user = :user')
->orderBy('w.geckoId ASC')
->getQuery();

$query->setParameter(':user', $user);

$entities = $query->getResult();

如果你想额外订购,你可以这样做:

->orderBy('w.geckoId ASC, w.weight ASC, w.id ASC')

关于php - 如何在 Symfony2 中使用 Controller 中的 EntityManager 对结果进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30458866/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com