嵌入表单集合错误:无法确定属性的访问类型 [英] Embed a Collection of Forms Error: Could not determine access type for property
问题描述
根据标签表单的集合嵌入到服务
表单中://symfony.com/doc/current/form/form_collections.htmlrel =nofollow noreferrer>本教程。 标签
和服务
实体有多对多关系。
表单正确呈现。但是当我提交表单时,我得到
无法确定属性tagList的访问类型
< blockquote>
错误。我不明白为什么新的
标签
对象未添加到服务
类中,通过调用addTag()
方法。
ServiceType
public function buildForm(FormBuilderInterface $ builder,array $ options)
{
$ builder
- > add('title',TextType :: class,array(
'label'=>'标题'
))
;
$ builder-> add('tagList',CollectionType :: class,array(
'entry_type'=> TagType :: class,
'allow_add'=> ; true,
'allow_delete'=> true,
'by_reference'=> false
)));
}
服务类
{
....
/ **
* @ ORM\ManyToMany(targetEntity =Tag,mappedBy =serviceList = {persist})
* /
private $ tagList;
/ **
* @return ArrayCollection
* /
public function getTagList()
{
return $ this-> tagList ;
}
/ **
* @param标签$标签
* @return服务
* /
public function addTag(Tag $标签)
{
if($ this-> tagList-> contains($ tag)== false){
$ this-> tagList-> add($ tag) ;
$ tag-> addService($ this);
}
}
/ **
* @param标签$标签
* @return服务
* /
public函数removeTag(Tag $ tag)
{
if($ this-> tagList-> contains($ tag)){
$ this-> tagList-> removeElement标签);
$ tag-> removeService($ this);
}
return $ this;
}
}
标签类
{
/ **
* @ ORM\ManyToMany(targetEntity =Service,inversedBy =tagList)
* @ ORM\JoinTable(name =tags_services)
* /
private $ serviceList;
/ **
* @param服务$ service
* @return标签
* /
public function addService(Service $ service)
{
if($ this-> serviceList-> contains($ service)== false){
$ this-> serviceList-> add($ service);
$ service-> addTag($ this);
}
return $ this;
}
/ **
* @param服务$ service
* @return标签
* /
public function removeService(Service $服务)
{
if($ this-> serviceList-> contains($ service)){
$ this-> serviceList-> removeElement($ service);
$ service-> removeTag($ this);
}
return $ this;
}
}
ServiceController
public function newAction(Request $ request)
{
$ service = new Service();
$ form = $ this-> createForm('AppBundle\Form\ServiceType',$ service);
$ form-> handleRequest($ request);
if($ form-> isSubmitted()&& $ form-> isValid()){
$ em = $ this-> getDoctrine ) - > getManager();
$ em-> persist($ service);
$ em-> flush();
return $ this-> redirectToRoute('service_show',array('id'=> $ service-> getId()));
}
return $ this-> render('AppBundle:Service:new.html.twig',array(
'service'=> $ service,
'form'=> $ form-> createView(),
));
}
解决方案请你尝试实现来自此网址的代码?
首先,请尝试更改映射/逆向,并从
Tag :: addService
方法中删除$ service-> addTag($ this);
/ p>I am trying to embed collection of
Tag
forms toService
form, according to this tutorial.Tag
andService
entities have many-to-many relationship.Form is rendering correctly. But when I submit form, I get
Could not determine access type for property "tagList"
error. I don't understand why new
Tag
object is not added to theService
class by calling theaddTag()
method.ServiceType
public function buildForm(FormBuilderInterface $builder, array $options) { $builder ->add('title', TextType::class, array( 'label' => 'Title' )) ; $builder->add('tagList', CollectionType::class, array( 'entry_type' => TagType::class, 'allow_add' => true, 'allow_delete' => true, 'by_reference' => false ))); }
Service class
{ .... /** * @ORM\ManyToMany(targetEntity="Tag", mappedBy="serviceList",cascade={"persist"}) */ private $tagList; /** * @return ArrayCollection */ public function getTagList() { return $this->tagList; } /** * @param Tag $tag * @return Service */ public function addTag(Tag $tag) { if ($this->tagList->contains($tag) == false) { $this->tagList->add($tag); $tag->addService($this); } } /** * @param Tag $tag * @return Service */ public function removeTag(Tag $tag) { if ($this->tagList->contains($tag)) { $this->tagList->removeElement($tag); $tag->removeService($this); } return $this; } }
Tag class
{ /** * @ORM\ManyToMany(targetEntity="Service", inversedBy="tagList") * @ORM\JoinTable(name="tags_services") */ private $serviceList; /** * @param Service $service * @return Tag */ public function addService(Service $service) { if ($this->serviceList->contains($service) == false) { $this->serviceList->add($service); $service->addTag($this); } return $this; } /** * @param Service $service * @return Tag */ public function removeService(Service $service) { if ($this->serviceList->contains($service)) { $this->serviceList->removeElement($service); $service->removeTag($this); } return $this; } }
ServiceController
public function newAction(Request $request) { $service = new Service(); $form = $this->createForm('AppBundle\Form\ServiceType', $service); $form->handleRequest($request); if ($form->isSubmitted() && $form->isValid()) { $em = $this->getDoctrine()->getManager(); $em->persist($service); $em->flush(); return $this->redirectToRoute('service_show', array('id' => $service->getId())); } return $this->render('AppBundle:Service:new.html.twig', array( 'service' => $service, 'form' => $form->createView(), )); }
解决方案Could you please try to implement code from this URL?
First, please try to change mapped/inverse sides, and remove
$service->addTag($this);
fromTag::addService
method.这篇关于嵌入表单集合错误:无法确定属性的访问类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!