doctrine2 – 嵌入表单集合错误:无法确定属性的访问类型

前端之家收集整理的这篇文章主要介绍了doctrine2 – 嵌入表单集合错误:无法确定属性的访问类型前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
根据 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(),));
}

解决方法

你可以尝试从这个URL实现代码吗?

http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/association-mapping.html#owning-and-inverse-side-on-a-manytomany-association

首先,请尝试更改映射/反面,并删除$ service-> addTag($ this);来自Tag :: addService方法

原文链接:https://www.f2er.com/html/232809.html

猜你在找的HTML相关文章