【问题标题】:Doctrine assignment error: Cannot assign Doctrine\ORM\PersistentCollection to propertyDoctrine 分配错误:无法将 Doctrine\ORM\PersistentCollection 分配给属性
【发布时间】:2021-03-18 17:12:52
【问题描述】:

我有一个上传文件,我想显示数据库中的所有类别,但我一直收到 EntityType 错误,我不知道为什么以前可以工作。

这是错误:Cannot assign Doctrine\ORM\PersistentCollection to property App\Entity\Category::$posts of type Doctrine\Common\Collections\ArrayCollection

<?php

declare(strict_types=1);

namespace App\Entity;

use App\Repository\CategoryRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity(repositoryClass=CategoryRepository::class)
 */
class Category
{
    /**
     * @ORM\Id
     * @ORM\GeneratedValue
     * @ORM\Column(type="integer")
     */
    private int $id;

    /**
     * @ORM\Column(type="string", length=255)
     */
    private string $name;

    /**
     * @ORM\OneToMany(targetEntity=Post::class, mappedBy="Category_id")
     */
    private ArrayCollection $Posts;

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

    public function getId(): ?int
    {
        return $this->id;
    }

    public function getName(): ?string
    {
        return $this->name;
    }

    public function setName(string $name): self
    {
        $this->name = $name;

        return $this;
    }

    /**
     * @return Collection|Post[]
     */
    public function gePosts(): Collection
    {
        return $this->posts;
    }

    public function addPost(Post $post): self
    {
        if (!$this->posts->contains($post)) {
            $this->$posts[] = $posts;
            $post->setCategoryId($this);
        }

        return $this;
    }

    public function removePost(Post $post): self
    {
        if ($this->posts->removeElement($post)) {
            // set the owning side to null (unless already changed)
            if ($post->getCategoryId() === $this) {
                $post->setCategoryId(null);
            }
        }

        return $this;
    }
    public function __toString(): string
    {
        return (string) $this->getName();
    }
}

我收到错误的上传表单

<?php

declare(strict_types=1);

namespace App\Form\Post;

use App\Entity\Category;
use App\Entity\Post;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\FileType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Validator\Constraints\File;
use Symfony\Component\Validator\Constraints\NotBlank;

class UploadType extends AbstractType
{
    /**
     * @param \Symfony\Component\Form\FormBuilderInterface $builder
     * @param array                                        $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('title', TextType::class, [
                
                'constraints' => [
                    new NotBlank([
                        'message' => 'Please enter a valid Title. '
                    ])
                ]
            ])
            ->add('category', EntityType::class, [
                'mapped' => false,
                'class' => Category::class,
                'choice_label' => 'name',
                'expanded' => true
            ])
            ->add('poster', FileType::class, [
                'required' => false,
                'constraints' => [
                    new File([
                        'maxSize' => '6000K',
                        'mimeTypes' => [
                            'image/x-png',
                            'image/jpeg',
                        ],
                        'mimeTypesMessage' => 'Please upload a image file.'
                    ])
                ]
            ])
            ->add('description', TextareaType::class, [
                'constraints' => [
                    new NotBlank([
                        'message' => 'Please enter a valid Title. '
                    ])
                ]
            ])
            ->add('mediainfo', TextareaType::class, [
                'required' => false,
            ]);
    }
    
    /**
     * @param \Symfony\Component\OptionsResolver\OptionsResolver $resolver
     */
    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults([
            'data_class' => Post::class,
        ]);
    }
}

在 Post 实体中

   /**
     * @ORM\ManyToOne(targetEntity=Category::class, inversedBy="posts")
     */
    private Category $Category_id;

环境信息:

  • Symfony 5.2.5(环境:开发,调试:真)
  • PHP 版本:8.0.3
  • 数据库驱动和版本:8.0.23-0ubuntu0.20.04.1

【问题讨论】:

    标签: php symfony doctrine-orm


    【解决方案1】:

    经过一番挖掘,发现将 ArrayCollection 更改为 Collection 的答案解决了我的问题

    改变:

    /**
     * @ORM\OneToMany(targetEntity=Post::class, mappedBy="Category_id")
     */
    private \Doctrine\Common\Collections\ArrayCollection $posts;
    

    到:

    /**
     * @ORM\OneToMany(targetEntity=Post::class, mappedBy="Category_id")
     */
    private \Doctrine\Common\Collections\Collection $posts;
    

    【讨论】:

    • 今天是 2021 年 11 月 4 日,你刚刚从不眠之夜拯救了我的生命。非常感谢@kunwara。这让我很开心!
    【解决方案2】:

    问题在于类型提示(ArrayCollection => Collection)

    改变:

    /**
     * @ORM\OneToMany(targetEntity=Post::class, mappedBy="Category_id")
     */
    private ArrayCollection $Posts;
    
    public function __construct()
    {
        $this->posts = new ArrayCollection();
    }
    

    作者:

    /**
     * @ORM\OneToMany(targetEntity=Post::class, mappedBy="Category_id")
     */
    private Collection $Posts;
    
    public function __construct()
    {
        $this->posts = new ArrayCollection();
    }
    

    【讨论】:

      【解决方案3】:

      $this-&gt;$posts[] = $posts;

      看起来这行应该是:

      $this-&gt;$posts[] = $post;

      【讨论】:

        猜你喜欢
        • 2012-09-23
        • 1970-01-01
        • 1970-01-01
        • 2017-04-11
        • 2021-04-22
        • 1970-01-01
        • 1970-01-01
        • 2018-12-07
        • 1970-01-01
        相关资源
        最近更新 更多