Embed a Collection of Forms Error: Could not determine access type for property

后端 未结 8 1299
悲哀的现实
悲哀的现实 2021-02-18 22:34

I am trying to embed collection of Tag forms to Service form, according to this tutorial. Tag and Service entities have many-

相关标签:
8条回答
  • 2021-02-18 22:49

    If you are using symfony, and use EntityRepository instead of CollectionType, make sure you use the 'multiple' => true, on your form build, otherwise the input will be for one entity and not for many, therefore it will call the setTagList instead of using the methods addTagList and removeTagList.

    0 讨论(0)
  • 2021-02-18 22:50

    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.

    0 讨论(0)
  • 2021-02-18 22:53

    This seems like an error with your constructor. Try this :

    public function __construct()
    {
        $this-> tagList = new \Doctrine\Common\Collections\ArrayCollection();
    }
    
    0 讨论(0)
  • 2021-02-18 23:03

    Short version:

    I just ran into this problem and solved it by adding a setter for the affected property:

    Could not determine access type for property "tagList"

    public function setTagList(Array $tagList)
    {
        $this->tagList = $tagList;
    }
    

    Long version:

    The error message is signaling that Symfony is trying to modify the object's state, but cannot figure out how to actually make the change due to the way its class is set up.

    Taking a look at Symfony's internals, we can see that Symfony gives you 5 chances to give it access and picks the best one in this order from top to bottom:

    1. A setter method named setProperty() with one argument:

    This is the first thing Symfony checks for and is the most explicit way to achieve this. As far as I'm aware this is the best practice:

    class Entity {
    
        protected $tagList;
    
        //...
    
        public function getTagList()
        {
            return $this->tagList;
        }
    
        //...
    }
    
    1. A combined getter and setter in one method with one argument:

    It's important to realize that this method will also be accessed by Symfony in order to get the object's state. Since those method calls don't include an argument, the argument in this method must be optional.

    class Entity {
    
        protected $tagList;
    
        //...
    
        public function tagList($tags = null)
        {
            if($reps){
                $this->tagList = $tags;
            } else {
                return $this->tagList;
            }
        }
    
        //...
    }
    
    1. The affected property being declared as public:

      class Entity {
      
          public $tagList;
          //... other properties here
      }
      
    2. A __set magic method:

    This will affect all properties rather than just the one you intended.

    class Entity {
    
        public $tagList;
    
        //...
    
        public function __set($name, $value){
            $this->$name = $value;
        }
        //...
    }
    
    1. A __call magic method (in some cases):

    I wasn't able to confirm this, but the internal code suggests this is possible when magic is enabled on PropertyAccessor's construction.


    Only using one of the above strategies is required.

    0 讨论(0)
  • 2021-02-18 23:05

    It's a long shot, but looking at your annotations I think the problem might be related to your manyToMany relationship. Try to change the owning side and inverse side (Swap the relationship) unless you specifically need to update from both ends (In that case I think the only solution is to add the objects manually or use oneToMany relationships).

    Changes made only to the inverse side of an association are ignored. Make sure to update both sides of a bidirectional association (or at least the owning side, from Doctrine’s point of view)

    This is a problem related to Doctrine I have suffered before, see: http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/unitofwork-associations.html

    0 讨论(0)
  • 2021-02-18 23:08

    Maybe you forgot in the __construct() of Service class and Tag class to initialize $tagList and $serviceList like this ?

    $this->tagList = new ArrayCollection();

    $this->serviceList = new ArrayCollection();

    0 讨论(0)
提交回复
热议问题