Symfony2,关系manyToMany in a Form [英] Symfony2, relation manyToMany in a Form
本文介绍了Symfony2,关系manyToMany in a Form的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我开始学习sf2,很酷,对于我的问题我有两张表:
媒体
/ **
* @ ORM\ManyToMany(targetEntity =Test\SiteBundle\Entity\Website,inversedBy =medias )
* @ ORM\JoinTable(name =media_website)
private $网站;
和网站
/ **
* @ ORM\ManyToMany(targetEntity =Test\SiteBundle\Entity\Media,mappedBy =websites)
private $ medias;
在我的MediaType.php中我有:
$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ b b b b b b b b b b b b b b b b b b b b 'class'=>'TestSiteBundle:Website',
'property'=>'name',
'query_builder'=> function(WebsiteRepository $ er)use($ user_id){
return $ er-> getMyWebsites($ user_id);
},
'multiple'=> false))
最后,在twig页面我有这样的:
< div class = 形式的基团 >
{{form_label(form.description,Description,{'label_attr':{'class':'control-label col-md-2'}})}}
< div class = COL-MD-5 >
{{form_widget(form.description,{'attr':{'class':'form-control'}})}}
< / div>
< / div>
当我尝试添加媒体时,我有这个错误:
属性网站也不存在方法setWebsites(),__set()或__call(),并且在类中具有公共访问权限Test\SiteBundle\Entity\Media。
任何帮助?非常感谢你。
解决方案
我发现,对于有同样问题的人,您的FormType中有 multiple => true ,所以我的MediaType应该是:
$ builder
- > add('websites','entity',array(
'class'=>'EveadSiteBundle:Website',
'property'=>'name' ,
'query_builder'=> function(WebsiteRepository $ er)use($ user_id){
return $ er-> getMyWebsites($ user_id);
},
' multiple'=> true))
I start learning sf2, pretty cool, for my problem I have two tables:
Media
/**
* @ORM\ManyToMany(targetEntity="Test\SiteBundle\Entity\Website", inversedBy="medias")
* @ORM\JoinTable(name="media_website")
private $websites;
and Website
/**
* @ORM\ManyToMany(targetEntity="Test\SiteBundle\Entity\Media", mappedBy="websites")
private $medias;
In my MediaType.php I have this:
$builder
->add('title')
->add('website', 'entity', array(
'class' => 'TestSiteBundle:Website',
'property' => 'name',
'query_builder' => function(WebsiteRepository $er)use($user_id) {
return $er->getMyWebsites($user_id);
},
'multiple'=>false))
finally, in the twig page I have this:
<div class="form-group">
{{ form_label(form.description, "Description", { 'label_attr': {'class': 'control-label col-md-2'} }) }}
<div class="col-md-5">
{{ form_widget(form.description, { 'attr': {'class': 'form-control'} }) }}
</div>
</div>
When I try to add a Media I have this error:
Neither the property "websites" nor one of the methods "setWebsites()", "__set()" or "__call()" exist and have public access in class "Test\SiteBundle\Entity\Media".
Any help ? and many thanks to you.
解决方案
I found it, for persons who have the same problem, in the relation ManyToMany you need to have multiple=>true in your FormType, so my MediaType should be:
$builder
->add('websites', 'entity', array(
'class' => 'EveadSiteBundle:Website',
'property' => 'name',
'query_builder' => function(WebsiteRepository $er)use($user_id) {
return $er->getMyWebsites($user_id);
},
'multiple'=>true))
这篇关于Symfony2,关系manyToMany in a Form的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文