Could not determine access type for property “file”

前端 未结 4 1834
走了就别回头了
走了就别回头了 2020-12-06 11:44

I have a little problem with my image upload, can u help me please:

Could not determine access type for property \"file\".

相关标签:
4条回答
  • 2020-12-06 12:11

    Please add "'mapped' => false," to form builder.

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
       ->add('file', FileType::class, 
         array(
            'data_class' => null,
            'mapped' => false,            
        ))
        ->add('name', TextType::class)
        ;
     }
    

    Those who say that it is wrong to dissolve, see the test there. I'm not the one who made it wrong.

    Test code: https://github.com/symfony/property-access/blob/master/Tests/PropertyAccessorCollectionTest.php#L151

    A second solution is to add function setXxx to the property that gives an error in class Entity.

    public $xxx;
    
    public function setXxx(Array $xxx)
    {
        $this->xxx = $xxx;
    }
    

    Or

    public function __construct()
    {
        $this->xxx = new ArrayCollection();
    }
    

    Video Link: https://knpuniversity.com/screencast/doctrine-relations/create-genus-note

    My English is bad, I can tell you.

    0 讨论(0)
  • 2020-12-06 12:18

    Stumbled on this while searching solution for my problem, that was shooting the same error and I was using also ArrayCollection class, so this may be helpful to someone:

    My field in ArrayCollection is called $files, so I had to add constructor like this:

    use Doctrine\Common\Collections\ArrayCollection;
    
    public function __construct()
    {
        $this->files = new ArrayCollection();
    }
    

    Then I added add and remove methods, like this:

    public function addFile(MyFile $file) : self
    {
        $file->setParentMyItem($this); // Setting parent item 
        $this->files->add($file);
        return $this;
    }
    
    public function removeFile(MyFile $file) : self
    {
        $this->files->removeElement($file);
        return $this;
    }
    

    But catch is that even my field name was $files I had to name add and remove methods addFile() and removeFile(), without 's' at end, which is totally not logical to me, but that solved the problem.

    0 讨论(0)
  • 2020-12-06 12:21

    Same error for me but on a OneToMany relation. Although setting "mapped => false" also solved the error there is another option. I only had a getter, adder and remover method on the entity. I needed to add a "setter" too

    In my case it was:

    /**
     * @param ArrayCollection|SubStatusOptions[]
     */
    public function setSubStatusOptions(ArrayCollection $subStatusOptions)
    {
        $this->subStatusOptions = $subStatusOptions;
    }
    
    0 讨论(0)
  • 2020-12-06 12:23

    Setting mapped => false is not the real solution, because the field IS mapped to the entity.

    In my case, I have a OneToMany relation, so I need the field mapped in order to use the 'cascase' => {"all"} option.

    If the field is not mapped, then you must to persist the related entity manually. That's no good.

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