doctrine-orm - 嵌入表单集合错误 : Could not determine access type for property

标签 doctrine-orm symfony symfony-forms

我正在尝试嵌入 Tag 的集合表格至Service表格,根据 this tutorial . TagService实体具有多对多关系。

表单正在正确呈现。但是当我提交表格时,我得到

Could not determine access type for property "tagList"



错误。我不明白为什么新 Tag对象未添加到 Service通过调用 addTag() 来上课方法。

服务类型
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;
}
 }

服务 Controller
  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方法。

关于doctrine-orm - 嵌入表单集合错误 : Could not determine access type for property,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42246686/

相关文章:

php - 如何从 Symfony2 控制台命令使用主/从 doctrine2 配置

php - Symfony2 - 在 Twig 中获取一个实体而不是 PersistentCollection

doctrine - 在 Doctrine 2 中查找或创建(更新插入)功能

php - Silex,您可以检查路由是否与回调匹配吗?

php - 如何使用 Symfony2 实现搜索过滤器表单

Symfony2 : Doctrine : PHPUnit : Set entity Id during flushing with mocked entity manager in unit tests

symfony - 最佳实践 Composer 和 Symfony 包

php - 如何在PHPUnit中获取表单对象

php - 基于 Symfony2 中其他字段值的字段条件验证

php - CollectionType 中的 EntityType : Get current object inside query_builder