我使用symfony2.7并且当我想将entitymanager注入formtype时我遇到了问题,但是我没有看到__construct()在我的服务上被调用,并且注入不起作用。 我使用服务将entitymanager注入我的formtype中。
public function createValeurChampNomenclatureAction(Request $request) {
$em = $this->getDoctrine()->getManager();
$cgaValeurChampNomenclature = new CGAValeurchampnomenclature();
$form = $this->createForm('espritApp_nomenclaturebundle_cgavaleurchampnomenclature', $CGAValeurChampNomenclature);
....
在我的formtype中我创建了我的构造
class CGAValeurChampNomenclatureType extends AbstractType
{
/**
* @var EntityManager $entityManager Entity manager
*/
private $em;
/**
* Constructor
*
* @param EntityManager $entityManager Entity manager
*/
public function _construct(EntityManager $entityManager)
{
$this->em=$entityManager;
}
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('cgatypenome','entity', array(
'class' => 'EspritAppNomenclatureBundle:CGATypenomenclature',
'property' => 'codeStr',
'multiple' => false,
'required' => true,
'attr'=> array('class'=>'form-control select2 codeStr'))
)
->add('cgacodenomenclature','choice', array('required' => TRUE,
'attr' => array('placeholder' => 'Code nomenclature','class'=>'form-control select2 codeNomenclature')
))
->add('cgachamptypenome','choice', array('required' => TRUE,
'attr' => array('placeholder' => 'Code Champ','class'=>'form-control select2 codeChampNomenclature')
))
->add('chaCodeStr','text', array('required' => TRUE,
'attr' => array('placeholder' => 'Champ code Str','class'=>'form-control')
))
->add('valeur','text', array('required' => TRUE,
'attr' => array('placeholder' => 'valeur','class'=>'form-control')
));
$formModif = function (FormInterface $form, $codeStr)
{
$listChampTypeNome = $this->em->getRepository('EspritAppNomenclatureBundle:CGAChamptypenomenclature')->getListChampTypeNomenclatureByCodeStr($codeStr);
$listCodeNome = $this->em->getRepository('EspritAppNomenclatureBundle:CGACodenomenclature')->getListCodeNomenclatureByCodeStr($codeStr);
if ($listChampTypeNome) {
$ChampsTypeNome = array();
foreach($listChampTypeNome as $ChampTypeNome) {
$ChampsTypeNome[] = $ChampTypeNome->getCodeChamp();
}
} else {
$ChampsTypeNome = null;
}
if ($listCodeNome) {
$CodesNome = array();
foreach($listCodeNome as $CodeNome) {
$CodesNome[] = $CodeNome->getCodeNome();
}
} else {
$CodesNome = null;
}
$form->add('cgacodenomenclature','choice', array('required' => TRUE,
'attr' => array('placeholder' => 'Code nomenclature','class'=>'form-control select2 codeNomenclature'),'choices' =>$ChampsTypeNome)
)
->add('cgachamptypenome','choice', array('required' => TRUE,
'attr' => array('placeholder' => 'Code Champ','class'=>'form-control select2 codeChampNomenclature'),'choices'=>$CodesNome)
);
};
$builder->get('cgacodenomenclature')->addEventListener(FormEvents::POST_SUBMIT, function (FormEvent $event) use ($formModif){
$formModif($event->getForm()->getParent(),$event->getForm()->getData());
});
}
/**
* @param OptionsResolverInterface $resolver
*/
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'EspritApp\NomenclatureBundle\Entity\CGAValeurchampnomenclature'
));
}
/**
* @return string
*/
public function getName()
{
return 'espritApp_nomenclaturebundle_cgavaleurchampnomenclature';
}
}
这是我在services.yml的服务
app.form.type.cgavaleurchampnome:
class: EspritApp\NomenclatureBundle\Form\CGAValeurChampNomenclatureType
tags:
- { name: form.type, alias: espritApp_nomenclaturebundle_cgavaleurchampnomenclature }
arguments: [@doctrine.orm.entity_manager]
我遇到此错误的问题:
在非对象
上调用成员函数getRepository()
答案 0 :(得分:1)
我怀疑你需要一个use语句来访问$ em:
$em = $this->em;
$formModif = function (FormInterface $form, $codeStr ) use $em
{
die(get_class($em));
虽然令人费解的是你为什么要这样做。 http://php.net/manual/en/functions.anonymous.php
答案 1 :(得分:0)
您可以轻松地将表单定义为服务并在其中传递EntityManager。为此,您应该在表单类型中定义getName
函数,如:
/**
* {@inheritdoc}
*/
public function getName()
{
return 'my_form';
}
将此表单类型定义为服务:
app.form.task:
class: AppBundle\Form\Type\MyFormType
tags:
- { name: form.type, alias: my_form }
arguments:
- "@doctrine.orm.entity_manager"
之后,您可以在构造函数中获取EntityManager对象并以整体形式使用它。
/**
* @var EntityManager $entityManager Entity manager
*/
private $em;
/**
* Constructor
*
* @param EntityManager $entityManager Entity manager
*/
public function __construct(EntityManager $entityManager)
{
$this->em = $entityManager;
}
用法示例:
/**
* {@inheritdoc}
*/
public function finishView(FormView $view, FormInterface $form, array $options)
{
$users = $this->em->getRepository('AppBundle:User')->findAll();
$view->vars['users'] = $users;
}
此外,您现在可以在控制器中使用表单类型,如下所示:
$form = $this->createForm('my_form');
这就是你要找的东西吗?