嵌入表单集合错误:无法确定属性的访问类型 [英] Embed a Collection of Forms Error: Could not determine access type for property

查看:117
本文介绍了嵌入表单集合错误:无法确定属性的访问类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据标签表单的集合嵌入到服务表单中://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(),
));
}


解决方案

请你尝试实现来自此网址的代码?



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



首先,请尝试更改映射/逆向,并从 Tag :: addService 方法中删除 $ service-> addTag($ this); / p>

I am trying to embed collection of Tag forms to Service form, according to this tutorial. Tag and Service 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 the Service class by calling the addTag() 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?

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

First, please try to change mapped/inverse sides, and remove $service->addTag($this); from Tag::addService method.

这篇关于嵌入表单集合错误:无法确定属性的访问类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆