Created
September 29, 2012 14:48
-
-
Save predakanga/3804238 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Acme\UserBundle\Form\Type; | |
use JMS\DiExtraBundle\Annotation\FormType; | |
use Symfony\Component\OptionsResolver\Options; | |
use Symfony\Component\OptionsResolver\OptionsResolverInterface; | |
use Doctrine\ORM\EntityRepository; | |
use Symfony\Component\Form\AbstractType; | |
/** | |
* @FormType() | |
*/ | |
class UserWithRoleSelectorType extends AbstractType | |
{ | |
public function getParent() | |
{ | |
return "entity"; | |
} | |
public function getName() | |
{ | |
return "acme_user_with_role_selector"; | |
} | |
public function setDefaultOptions(OptionsResolverInterface $resolver) | |
{ | |
$resolver->setRequired(array('role')); | |
$resolver->setDefaults(array('class' => 'Acme\UserBundle\Entity\User', | |
'property' => 'username', | |
'query_builder' => function(Options $options, $value) { | |
$serRole = serialize($options['role']); | |
return function(EntityRepository $er) use($serRole) { | |
$searchStr = "%{$serRole}%"; | |
return $er->createQueryBuilder('u')->leftJoin('u.userClass', 'uc') | |
->leftJoin('uc.groups', 'g') | |
->where('g.roles LIKE :searchStr') | |
->setParameter('searchStr', $searchStr) | |
->orderBy('u.username', 'ASC'); | |
}; | |
})); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment