Doctrine分配错误:无法将DoctrineORMPersistentCollection分配给属性
我有一个上传文件,我想从那里显示数据库中的所有类别,但我一直收到 EntityType 错误,我不知道之前为什么工作。
这是错误: Cannot assign DoctrineORMPersistentCollection to property AppEntityCategory::$posts of type DoctrineCommonCollectionsArrayCollection
<?php
declare(strict_types=1);
namespace AppEntity;
use AppRepositoryCategoryRepository;
use DoctrineCommonCollectionsArrayCollection;
use DoctrineCommonCollectionsCollection;
use DoctrineORMMapping as ORM;
/**
* @ORMEntity(repositoryClass=CategoryRepository::class)
*/
class Category
{
/**
* @ORMId
* @ORMGeneratedValue
* @ORMColumn(type="integer")
*/
private int $id;
/**
* @ORMColumn(type="string", length=255)
*/
private string $name;
/**
* @ORMOneToMany(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 AppFormPost;
use AppEntityCategory;
use AppEntityPost;
use SymfonyBridgeDoctrineFormTypeEntityType;
use SymfonyComponentFormAbstractType;
use SymfonyComponentFormExtensionCoreTypeFileType;
use SymfonyComponentFormExtensionCoreTypeTextareaType;
use SymfonyComponentFormExtensionCoreTypeTextType;
use SymfonyComponentFormFormBuilderInterface;
use SymfonyComponentOptionsResolverOptionsResolver;
use SymfonyComponentValidatorConstraintsFile;
use SymfonyComponentValidatorConstraintsNotBlank;
class UploadType extends AbstractType
{
/**
* @param SymfonyComponentFormFormBuilderInterface $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 SymfonyComponentOptionsResolverOptionsResolver $resolver
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => Post::class,
]);
}
}
在 Post 实体中
/**
* @ORMManyToOne(targetEntity=Category::class, inversedBy="posts")
*/
private Category $Category_id;
环境信息:
- Symfony 5.2.5 (env: dev, debug: true)
- PHP 版本:8.0.3
- 数据库驱动和版本:8.0.23-0ubuntu0.20.04.1
回答
经过一番挖掘找到答案,将 ArrayCollection 更改为 Collection 解决了我的问题
change:
/**
* @ORMOneToMany(targetEntity=Post::class, mappedBy="Category_id")
*/
private DoctrineCommonCollectionsArrayCollection $posts;
to:
/**
* @ORMOneToMany(targetEntity=Post::class, mappedBy="Category_id")
*/
private DoctrineCommonCollectionsCollection $posts;
THE END
二维码