我有一个实体数组,我想按国家/地区名称排序,由国际区域捆绑包翻译。国名作为两个字母的代码存储在数据库中,并借助实体的方法进行翻译。
class Activity {
// ...
/**
* @ORMColumn(type="string", length=2)
*/
private $country;
public function getCountryName()
{
return Intl::getRegionBundle()->getCountryName($this->country);
}
}
如何按国家/地区名称对一系列活动进行排序?我找不到用教义来做到这一点的方法,usort
也行不通。
$activities = $this->getDoctrine()->getRepository(Activity::class)->findAll();
// how do I order this by country name?
在实体存储库中进行自定义查询。类似的东西...
public function getCountryOrderedActivity() {
return $this->createQueryBuilder("c")
->orderBy("c.country", "ASC")
->getQuery()
->getOneOrNullResult();
}
然后在控制器中执行以下操作:
$this->getDoctrine()->getManager()->getRepository(Activity::class)->getCountryOrderedActivity();