【问题标题】:Symfony2: How call the repository of an entity in the FormTypeSymfony2:如何在 FormType 中调用实体的存储库
【发布时间】:2014-11-17 15:37:31
【问题描述】:

我尝试以我的实体BonCommande 的类形式调用我的实体Category 的存储库,但是出现了这个错误:

注意:未定义的属性:Application\VehiculeBundle\Form\BonCommandeType::$em 在 C:\wamp\www\Symfony_test\src\Application\VehiculeBundle\Form\BonCommandeType.php 第 74 行

这是我的代码:

班级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);
}
...
}

这是导致错误的指令:

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

【问题讨论】:

    标签: forms symfony repository


    【解决方案1】:

    FormComponent 是一个独立的组件,它不提供任何 entityManager 使用。如果你想使用它,你必须注入它或通过$options传递它..

    在您的情况下,如果您直接将其传递给类型的 __construct 或通过 $options 数组传递或将您的类型声明为服务并向其注入实体管理器,则将是正确的:

    class BonCommandeType extends AbstractType
    {
        private $em;
    
        public function __construct(EntityManagerInterface $em)
        {
            $this->em = $em;
        }
    
        ...
    }
    

    或

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

    【讨论】:

      【解决方案2】:

      从你的代码中我假设你错过了这个:

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

      请记住,当您创建一个新的表单对象时,您应该像这样使用 smth:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-02-10
        • 1970-01-01
        • 2013-02-22
        • 1970-01-01
        • 1970-01-01
        • 2013-04-20
        • 2013-05-02
        • 1970-01-01
        相关资源
        最近更新 更多