根据
this tutorial,我正在尝试将标签表单的集合嵌入到服务表单中。标签和服务实体有多对多的关系。
表单呈现正确。但是当我提交表单时,我无法确定属性“tagList”错误的访问类型。我不明白为什么没有通过调用addTag()方法将新的Tag对象添加到Service类中。
服务类型
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 ))); }
服务类
{ .... /** * @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; } }
标签类
{ /** * @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(),)); }