sgl*_*ard 2 symfony doctrine-orm symfony-2.1
我正在尝试在Symfony 2.1中显示带有optgroups的选择框.我的实体树是:客户端有项目,项目有部分(Part->getProject()->getClient())
我想以这种方式显示我的选择框:
<select>
<optgroup>Client name
<option>Part name</option>
<!-- ... -->
</optgroup>
<!-- ... -->
</select>
Run Code Online (Sandbox Code Playgroud)
Symfony doc没有太大帮助.我的工作表单构建器(没有group_by选项)给我一个简单的选择:
$this->createFormBuilder()
->add('part','entity',array(
'class' => 'SGLFLTSPartBundle:Part',
'property' => 'name',
'query_builder' => function (\SGL\FLTS\PartBundle\Entity\PartRepository $er) {
return $er->createQueryBuilder('p');
}))
->getForm();
Run Code Online (Sandbox Code Playgroud)
如何添加group_by选项以显示客户端名称?到目前为止我已经尝试过了
'group_by' => 'project.client.name'
'group_by' => 'project.client'
'group_by' => 'ppc.name' // the DQL table alias
Run Code Online (Sandbox Code Playgroud)
都给PHP错误
我也尝试将项目名称仅显示为optgroup,没有运气:
'group_by' => 'project'
'group_by' => 'project.name'
'group_by' => 'project.id' // throws no error, giving me <optgroup label="1"> ...
Run Code Online (Sandbox Code Playgroud)
并尝试在createQueryBuilder中添加项目/客户端连接
$er->createQueryBuilder('p')->select('p, pp')->leftJoin('p.project','pp');
$er->createQueryBuilder('p')->select('p, pp.name')->leftJoin('p.project','pp')
// wrong
Run Code Online (Sandbox Code Playgroud)
谢谢!
小智 6
我今天遇到了类似的问题.
我想你已经看到很多与错误使用Objects作为数组键有关的PHP错误了吗?这是由Symfony尝试将整个相关对象用作分组结果数组中的数组键引起的.
我需要进一步研究它以获得更好的解决方案,但与此同时这就是我正在使用的......
向被Part调用的实体添加一个新方法getClientName,如下所示:
public function getClientName()
{
// safety measure in-case a part hasn't been assigned to a project
if (null === $this->getProject()) {
return null;
}
// safety measure in-case a project hasn't been assigned to a client
if (null === $this->getProject()->getClient()) {
return null;
}
return $this->getProject()->getClient()->getName();
}
Run Code Online (Sandbox Code Playgroud)
在表单域构建器中将group_by选项设置为clientName:
$this->createFormBuilder()
->add('part','entity',array(
'class' => 'SGLFLTSPartBundle:Part',
// this property will be processed by Symfony as `$part->getClientName()`
'property' => 'clientName',
->getForm();
Run Code Online (Sandbox Code Playgroud)
使用这个额外方法背后的想法是给Symfony一个类方法,它可以调用它来获取一个字符串值来执行分组.
如果其他人有更优雅的解决方案,我会热衷于看到它.
| 归档时间: |
|
| 查看次数: |
5244 次 |
| 最近记录: |