【发布时间】:2016-05-31 13:48:14
【问题描述】:
我在 Symfony 2.8 中有一个表单类型,它使用 choice_label 选项。这个值是一个函数,它对参数应用一些格式,目的是返回字段name,任何前导“The”字符串移到末尾(因此“The Company Inc”变成“Company Inc, The”。
当choice_label 不是实体字段时,如何按FormType 排序?
// \Form\Type\AdvertiserType.php
...
->add(
'advertiser',
'entity',
array(
'class' => 'MyBundle:Advertiser',
'label' => 'Advertiser Account',
'choice_label' => 'formattedName',
'query_builder' => function(EntityRepository $repo) {
return $repo->createQueryBuilder('a')
->orderBy('a.name', 'ASC')
;
}
)
)
...
我不能在查询生成器中使用orderBy('a.formattedName', 'ASC'),因为这是一个函数名而不是实体字段。
广告商实体具有此附加功能:
// Entity\Advertiser.php
...
public function getFormattedName() {
if (substr($this->name, 0, 4) == 'The ') {
return substr($this->name, 4) . ', The';
} else {
return $this->name;
}
}
...
感谢您的帮助或指点。
【问题讨论】:
标签: php symfony doctrine-orm