Skip to content

Instantly share code, notes, and snippets.

@khepin
Created October 16, 2012 07:51
Show Gist options
  • Select an option

  • Save khepin/3897881 to your computer and use it in GitHub Desktop.

Select an option

Save khepin/3897881 to your computer and use it in GitHub Desktop.
<?php
namespace Acme\MessageBundle\FormType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Symfony\Component\Security\Core\SecurityContext;
use Doctrine\ODM\MongoDB\DocumentRepository;
class FriendMessageFormType extends AbstractType
{
private $user;
public function __construct(SecurityContext $security_context)
{
$this->user = $security_context->getToken()->getUser();
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
$user_id = $this->user->getId();
$form_options = [
'class' => 'Acme\MessageBundle\Document\User',
'multiple' => false,
'expanded' => false,
'property' => 'title',
// 'attr' => ['class' => 'select2'],
'query_builder' => function(DocumentRepository $dr) use ($user_id) {
return $dr->createQueryBuilder()->field('friends')->equals(new \MongoId($user_id));
}
];
$builder->add('friend', 'document', $form_options);
$builder
->add('subject', 'text')
->add('body', 'textarea')
;
}
public function getName()
{
return 'acme_friend_message';
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment