Symfony2 - 以 ORM DQL 的形式设置复选框的值
Symfony2 - Set value of checkboxes in form from ORM DQL
我正在创建一个表单,该表单应该使用 LexikFilter Bundle 在 Symfony2(2.3 版)中过滤自定义查询列表。
项目设置:
- Symfony2:2.3.7
- LexikFilter 捆绑包:3.0.8
- 条令 ORM 2.4
该项目由具有 n:m 关系的实体组成(Parents-Training),Parent Filter Form 嵌入了一个 Training 过滤器表单,如下所示:
class MyParentsType extends AbstractType
{
private $repository ;
function __construct($repository)
{
$this->repository = $repository; // store it, we are going to use it later
}
public function getName()
{
return 'parents_filter';
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('Firstname', 'filter_text', array('condition_pattern' => FilterOperands::STRING_BOTH,))
->add('Lastname', 'filter_text', array('condition_pattern' => FilterOperands::STRING_BOTH,))
->add('trainings','filter_collection_adapter', array(
'type'=> new MyTrainingType2($this->repository),
'default_data' => new ArrayCollection(),
'add_shared'=> function(FilterBuilderExecuterInterface $qbe){
$closure = function(QueryBuilder $filterBuilder, $alias, $joinAlias, Expr $expr){
$filterBuilder ->leftJoin($alias.'.options',$joinAlias);
};
$qbe->addOnce($qbe->getAlias().'.options','opt',$closure);
},
));
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(
[
'data_class' =>'tuto\LexikTestBundle\Entity\Parents',
'csrf_protection' => false,
'validation_groups' => ['filtering'] // avoid NotBlank() constraint-related message
]
);
}
和训练过滤器形式:
class MyTrainingType2 extends AbstractType
{
private $repository ;
function __construct($repository)
{
$this->repository = $repository; // store it, we are going to use it later
}
public function getName()
{
return 'training_filter';
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('Title', 'filter_text', array('condition_pattern' => FilterOperands::STRING_BOTH,))
->add('Location', 'filter_choice', array( 'choice_list' => new ChoiceList($this->repository->getLocationList(),$this->repository->getLocationList()),
'expanded'=> true,
'multiple'=>true,
)
);
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(
[
'data_class' =>'tuto\LexikTestBundle\Entity\Training',
'csrf_protection' => false,
'validation_groups' => ['filtering'] // avoid NotBlank() constraint-related message
]
);
}
}
这里是Training Repository
的getLocationList()
方法:
public function getLocationList()
{ $qb = $this->createQueryBuilder('t')
->select('t.Location')//
->orderBy('t.Location','asc')
->distinct(true);
return $qb->getQuery()->getArrayResult();
}
过滤器确实显示复选框,但值字段设置为列名 Location
。这里是 getArrayResult
返回的数组片段
array (size=199)
0 =>
array (size=1)
'Location' => string '‘Arīshah' (length=11)
1 =>
array (size=1)
'Location' => string 'Abuja' (length=5)
我希望将其返回为:
0 =>
array (size=1)
'‘Arīshah' => string '‘Arīshah' (length=11)
1 =>
array (size=1)
'Abuja' => string 'Abuja' (length=5)
你能试试吗:
$locations = array_map(function(array $location){
return $location['Location'];
}, $this->repository->getLocationList());
// and then in your form definition:
'choice' => ['choices' => array_combine($locations, $locations)]
我正在创建一个表单,该表单应该使用 LexikFilter Bundle 在 Symfony2(2.3 版)中过滤自定义查询列表。
项目设置:
- Symfony2:2.3.7
- LexikFilter 捆绑包:3.0.8
- 条令 ORM 2.4
该项目由具有 n:m 关系的实体组成(Parents-Training),Parent Filter Form 嵌入了一个 Training 过滤器表单,如下所示:
class MyParentsType extends AbstractType
{
private $repository ;
function __construct($repository)
{
$this->repository = $repository; // store it, we are going to use it later
}
public function getName()
{
return 'parents_filter';
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('Firstname', 'filter_text', array('condition_pattern' => FilterOperands::STRING_BOTH,))
->add('Lastname', 'filter_text', array('condition_pattern' => FilterOperands::STRING_BOTH,))
->add('trainings','filter_collection_adapter', array(
'type'=> new MyTrainingType2($this->repository),
'default_data' => new ArrayCollection(),
'add_shared'=> function(FilterBuilderExecuterInterface $qbe){
$closure = function(QueryBuilder $filterBuilder, $alias, $joinAlias, Expr $expr){
$filterBuilder ->leftJoin($alias.'.options',$joinAlias);
};
$qbe->addOnce($qbe->getAlias().'.options','opt',$closure);
},
));
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(
[
'data_class' =>'tuto\LexikTestBundle\Entity\Parents',
'csrf_protection' => false,
'validation_groups' => ['filtering'] // avoid NotBlank() constraint-related message
]
);
}
和训练过滤器形式:
class MyTrainingType2 extends AbstractType
{
private $repository ;
function __construct($repository)
{
$this->repository = $repository; // store it, we are going to use it later
}
public function getName()
{
return 'training_filter';
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('Title', 'filter_text', array('condition_pattern' => FilterOperands::STRING_BOTH,))
->add('Location', 'filter_choice', array( 'choice_list' => new ChoiceList($this->repository->getLocationList(),$this->repository->getLocationList()),
'expanded'=> true,
'multiple'=>true,
)
);
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(
[
'data_class' =>'tuto\LexikTestBundle\Entity\Training',
'csrf_protection' => false,
'validation_groups' => ['filtering'] // avoid NotBlank() constraint-related message
]
);
}
}
这里是Training Repository
的getLocationList()
方法:
public function getLocationList()
{ $qb = $this->createQueryBuilder('t')
->select('t.Location')//
->orderBy('t.Location','asc')
->distinct(true);
return $qb->getQuery()->getArrayResult();
}
过滤器确实显示复选框,但值字段设置为列名 Location
。这里是 getArrayResult
array (size=199)
0 =>
array (size=1)
'Location' => string '‘Arīshah' (length=11)
1 =>
array (size=1)
'Location' => string 'Abuja' (length=5)
我希望将其返回为:
0 =>
array (size=1)
'‘Arīshah' => string '‘Arīshah' (length=11)
1 =>
array (size=1)
'Abuja' => string 'Abuja' (length=5)
你能试试吗:
$locations = array_map(function(array $location){
return $location['Location'];
}, $this->repository->getLocationList());
// and then in your form definition:
'choice' => ['choices' => array_combine($locations, $locations)]