2014-11-17 107 views
0

我試着打電話給我的實體Category的倉庫在我的實體BonCommande的類形式,但這個錯誤ocuured:Symfony2中:如何調用一個實體的存儲庫中FormType

Notice: Undefined property: Application\VehiculeBundle\Form\BonCommandeType::$em in C:\wamp\www\Symfony_test\src\Application\VehiculeBundle\Form\BonCommandeType.php line 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']); 

回答

1

從你的代碼,我認爲你缺少這樣的:

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

請記住,當你創建你應該使用不便像一個新的表單對象:

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

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]); 
相關問題