# security.yml
firewalls:
admin:
pattern: ^/admin/
anonymous: ~
logout:
path: /admin/logout
target: /admin/
form_login:
provider: jeka_user.provider
check_path: /admin/login_check
login_path: /admin/login
default_target_path: /admin/dashboard
always_use_default_target_path: true
context: primary_auth
main:
pattern: ^/
form_login:
provider: fos_user.provider
check_path: /login_check
login_path: /error/forbidden
use_referer: true
use_forward: true
failure_path: null
failure_forward: false
...
...
// трансформер
class ApplicationToIdTransformer implements DataTransformerInterface
{
private $dm;
function __construct(DocumentManager $dm)
{
$this->dm = $dm;
}
public function transform($applications)
{
$collection = array();
if (empty($applications)){
return $collection;
}
foreach($applications as $app){
$collection[]=$app->getId();
}
return $collection;
}
public function reverseTransform($ids)
{
if (!$ids){
return null;
}
$applications = $this->dm->createQueryBuilder('SMAppBundle:Application')
->field('id')->in($ids)
->getQuery()->execute();
if (empty($applications)) {
throw new TransformationFailedException(sprintf(
'An Application with ids "%s" does not exist!',
implode(',',$ids)
));
}
return $applications->toArray();
}
}
// FormType
class ProjectUserFormType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$transformer = new ApplicationToIdTransformer($options['dm']);
$builder->add(
$builder->create(
'applications',
'choice',
array(
'choices'=>$this->getApplicationChoices($options['dm']),
'expanded'=>true,
'multiple'=>true
)
)->addModelTransformer($transformer)
);
;
}
/**
* Returns the name of this type.
*
* @return string The name of this type
*/
public function getName()
{
return 'project';
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array('data_class' => 'SM\AppBundle\Document\Project'));
$resolver->setRequired(
array(
'dm',
)
);
$resolver->setAllowedTypes(
array(
'dm' => 'Doctrine\ODM\MongoDB\DocumentManager',
)
);
}
private function getApplicationChoices($dm) {
$choices = array();
foreach($dm->getRepository('SM\AppBundle\Document\Application')->findAll() as $app){
$choices[$app->getId()]=$app->getName();
}
return $choices;
}
}
// в контролере
$form = $this->createForm(new ProjectUserFormType(),null,array('dm' => $this->getDocumentManager()));