Skip to content

Instantly share code, notes, and snippets.

@davialexandre
Created June 2, 2016 20:23
Show Gist options
  • Save davialexandre/e3992f3ac165a588cf01aa1092070c6f to your computer and use it in GitHub Desktop.
Save davialexandre/e3992f3ac165a588cf01aa1092070c6f to your computer and use it in GitHub Desktop.
Using getRepository on a FormType
<?php
namespace AppBundle\Form\Type;
use Doctrine\Common\Persistence\ObjectManager;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
class NomeDoSeuTypeType extends AbstractType
{
/**
* @var ObjectManager
*/
private $objectManager;
public function __construct(ObjectManager $objectManager)
{
$this->objectManager = $objectManager;
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->addEventListener(
FormEvents::PRE_SUBMIT,
function(FormEvent $event) {
...
$this-objectManager->getRepository('YourEntity')
...
}
);
}
}
app.form.type.nome_do_seu_type:
class: AppBundle\Form\Type\NomeDoSeuTypeType
arguments: ['@doctrine.orm.entity_manager']
tags:
- { name: form.type, alias: nome_do_seu_type }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment