Symfony2: How call the repository of an entity in the FormType

廉价感情. 提交于 2019-12-04 06:46:40

问题


I tried to call the repository of my entity Category in the class form of my entity BonCommande, but this error ocuured:

Notice: Undefined property: Application\VehiculeBundle\Form\BonCommandeType::$em in C:\wamp\www\Symfony_test\src\Application\VehiculeBundle\Form\BonCommandeType.php line 74

This is my code:

The class BonCommandeType.php:

namespace Application\VehiculeBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Form\FormInterface;
use Application\VehiculeBundle\Entity\Category;

class BonCommandeType extends AbstractType
{
        /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
       public function buildForm(FormBuilderInterface $builder, array $options)
    {
        // Name of the user
        $builder->add('observation', 'text');


    /* Add additional fields... */

    $builder->add('save', 'submit');

    // Add listeners
    $builder->addEventListener(FormEvents::PRE_SET_DATA, array($this, 'onPreSetData'));
    $builder->addEventListener(FormEvents::PRE_SUBMIT, array($this, 'onPreSubmit'));
}


protected function addElements(FormInterface $form, Category $categorie = null) {
    // Remove the submit button, we will place this at the end of the form later
    $submit = $form->get('save');
    $form->remove('save');


    // Add the province element
    $form->add('categorie', 'entity', array(
        'data' => $categorie,
        'empty_value' => '-- Choose --',
        'class' => 'ApplicationVehiculeBundle:Category',
        'property' => 'intitule',
        'mapped' => false)
    );

    // Cities are empty, unless we actually supplied a province
    $vehicules = array();
    if ($categorie) {
        // Fetch the cities from specified province
        $repo = $this->em->getRepository('ApplicationVehiculeBundle:Vehicule');
        $cities = $repo->findByCategory($categorie);
    }

    // Add the city element
    $form->add('vehicule', 'entity', array(
        'empty_value' => '-- Select a categorie first --',
        'class' => 'ApplicationVehiculeBundle:Vehicule',
        'choices' => $vehicules,
    ));

    // Add submit button again, this time, it's back at the end of the form
    $form->add($submit);
}


function onPreSubmit(FormEvent $event) {
    $form = $event->getForm();
    $data = $event->getData();

    // Note that the data is not yet hydrated into the entity.
    $categorie = $this->em->getRepository('ApplicationVehiculeBundle:Category')->find($data['categorie']);
    $this->addElements($form, $categorie);
}


function onPreSetData(FormEvent $event) {
    $account = $event->getData();
    $form = $event->getForm();

    // We might have an empty account (when we insert a new account, for instance)
    $categorie = $account->getVehicule() ? $account->getVehicule()->getCategorie() : null;
    $this->addElements($form, $categorie);
}
...
}

This is the instruction that causes the error:

$categorie = $this->em->getRepository('ApplicationVehiculeBundle:Category')->find($data['categorie']);

回答1:


From your code I assume you are missing this:

//Somewhere at the begging of your BonCommandeType
protected $em;
...
public function __construct(EntityManager $em) 
{
    $this->em = $em;
}

Keep in mind that when you create a new form object you should use smth like :

BonCommandeType($this->getDoctrine()->getManager()) // if inside a controller



回答2:


FormComponent is an independent component and it doesn't provide any entityManager to use. You have to inject it or pass it by $options if you want to use it..

In your case it would be correct if you directly pass it to the type's __construct or pass by $options array or declare your type as a service and inject entity manager to it:

class BonCommandeType extends AbstractType
{
    private $em;

    public function __construct(EntityManagerInterface $em)
    {
        $this->em = $em;
    }

    ...
}

or

$this->createForm(TYPE, DATA, ['em' => $em]);


来源:https://stackoverflow.com/questions/26976226/symfony2-how-call-the-repository-of-an-entity-in-the-formtype

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!