Reputation: 1855
/Edited/
I have this class:
namespace Baza\BlogBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Doctrine\ORM\EntityManager;
class filterType extends AbstractType
{
protected $em;
public function __construct(EntityManager $em)
{
$this->em = $em;
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
$this->$em->getDoctrine()->getEntityManager();
/****
****/
}
}
And this is my services yml:
services:
filterType:
class: Baza\BlogBundle\Form\filterType
arguments: [doctrine.orm.entity_manager]
When I run the code I get following exception:
Catchable Fatal Error: Argument 1 passed to Baza\BlogBundle\Form\filterType::__construct() must be an instance of Doctrine\ORM\EntityManager, none given
I'm all out of ideas.
Upvotes: 1
Views: 6711
Reputation: 14703
I created the FormType myself. This should work:
<?php
// Baza\BlogBundle\Form\filterType.php
namespace Baza\BlogBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Doctrine\ORM\EntityManager;
class filterType extends AbstractType
{
protected $em;
public function __construct(EntityManager $em)
{
$this->em = $em;
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
// Do something with your Entity Manager using "$this->em"
}
public function getName()
{
return 'filter_type';
}
}
In your Controller use something like
<?php
// Baza\BlogBundle\Controller\PageController.php
namespace Baza\BlogBundle\Controller;
use Baza\BlogBundle\Form\filterType;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
class BaseController extends Controller
{
public function testEntityManager()
{
// assign whatever you need
$enquiry = null;
// getEntityManager() is depricated. Use getManager() instead.
$em = $this->getDoctrine()->getManager();
$this->createForm(
new filterType($em),
$enquiry
);
}
}
Never forget to include/use all the classes you are using. Otherwise PHP will assume the class is inside your currently used namespace.
That's why you got the error (on Cerad's post)
Catchable Fatal Error: Argument 1 passed to
Baza\BlogBundle\Form\filterType::__construct()
must be an instance of Baza\BlogBundle\Form\EntityManager [...]
As you didn't include the EntityManager PHP assumes it's a class inside your current namespace which was Baza\BlogBundle\Form
.
The funny looking Class EntityManager50ecb6f979a07_546a8d27f194334ee012bfe64f629947b07e4919\__CG__\Doctrine\ORM\EntityManager
is a Doctrine2 proxy class.
Since Symfony 2.1, calling $this->getDoctrine()->getEntityManager()
no lonoger results in a Doctrine\ORM\EntityManager
but a proxy class which in fact behaves just like the original EntityManager
and can be passed without problems.
Upvotes: 3
Reputation: 48893
The @ symbol is needed to indicate that the argument is a service. However, as you found out, @ trips up the yaml parser. The solution is to use quotes.
services:
filterType:
class: Baza\BlogBundle\Form\filterType
arguments: ['@doctrine.orm.entity_manager']
I remember it taking me a few hours to figure this out as well.
Upvotes: 1