在我的选择选项中,All是第一个选项。
PushBroadcastAdmin.php
protected function configureFormFields(FormMapper $formMapper)
{
if ($this->getSubject()->getId()) {
$data = $this->getSubject()->getUsers();
$recipientsArray = explode(',', $data);
}
else {
$recipientsArray = [];
}
$em = $this->modelManager->getEntityManager('AppBundle:User');
$users = $em->getRepository('AppBundle:User')->findAllUsers();
$formMapper
->add('recipients', ChoiceType::class, array(
'choices' => $users,
'data' => $recipientsArray,
'expanded' => false,
'multiple' => true,
'label' => 'Recipient(s)',
'attr' => array(
'class' => 'push_recipients_select',
),
))
->add('title')
->add('message')
;
}
public function prePersist($object)
{
$data = $object->getRecipients();
$object->setUsers(implode(',',$data));
parent::prePersist($object);
}
UserRepository.php
public function findAllUsers()
{
$qb = $this->createQueryBuilder('u');
$data = $qb
->select('u.id')
->addSelect('u.name')
->andWhere('u.enabled = 1')
->andWhere('u.isDeleted = 0')
->getQuery()
->getArrayResult();
$result = [];
$result['All'] = 'all';
foreach ($data as $value){
$result[$value['name']] = $value['id'];
}
return $result;
}