【问题标题】:Argument 1 passed to ArrayCollection::__construct() must be of the type array object given传递给 ArrayCollection::__construct() 的参数 1 必须是给定的数组对象类型
【发布时间】:2021-07-20 08:03:44
【问题描述】:

我在 Symfony 4.4 中编程。

我有一个带有 categories 属性的 Recipe 实体。当我尝试使用数据库中的表格制作食谱记录时,它给了我以下错误:

传递给 Doctrine\Common\Collections\ArrayCollection::__construct() 的参数 1 必须是数组类型,给定对象,在 C:\xampp\htdocs\proyecto_final_daw\vendor\doctrine\orm\lib\Doctrine\ 中调用ORM\UnitOfWork.php 第 675 行

我使用并需要多对多关系。我无法将这种类型更改为关系。

上传食谱类型表单

<?php

 namespace App\Form;

use App\Entity\Recipe;
use App\Entity\Category;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\Extension\Core\Type\FileType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\CallbackTransformer;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\CollectionType;

class UploadRecipesType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('title',TextType::class,[
                'label' => 'Titulo',
            ])
            ->add('image',FileType::class,[
                'label' => 'Imagen',
            ])
            ->add('description',TextareaType::class,[
                'label' => 'Descripción',
            ])
             ->add('categories',EntityType::class,[
                 'class' => Category::class,
                 'mapped' => true,
                 'label' => 'Categoria',
                'choice_label' => 'name',
             ])

    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults([
            'data_class' => Recipe::class,
        ]);
    }
}

Recipe.php 实体

<?php

namespace App\Entity;

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

/**
 * @ORM\Entity(repositoryClass=RecipeRepository::class)
 */
class Recipe
{
    public function __construct() {
        $this->categories = new \Doctrine\Common\Collections\ArrayCollection();
    }

    /**
     * @ORM\Id
     * @ORM\GeneratedValue
     * @ORM\Column(type="integer")
     */
    private $id;

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

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

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

    /** 
     * @ORM\ManyToOne(targetEntity="User", inversedBy="recipes")
     * @ORM\JoinColumn(nullable=false)
    */
    private $user;

    /**
     * @ORM\Column(type="integer", nullable=true)
     */
    private $score;

    /**
     * @ORM\Column(type="boolean")
     */
    private $visible;

    /**
     * @ORM\ManyToMany(targetEntity="Category", inversedBy="recipe")
     * @ORM\JoinTable(name="recipes_categories")
     */
    private $categories;

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

    public function setId(int $id)
    {
        $this->id = $id;

        return $this;
    }

    public function getTitle(): ?string
    {
        return $this->title;
    }

    public function setTitle(string $title): self
    {
        $this->title = $title;

        return $this;
    }

    public function getImage(): ?string
    {
        return $this->image;
    }

    public function setImage(string $image): self
    {
        $this->image = $image;

        return $this;
    }

    public function getDescription(): ?string
    {
        return $this->description;
    }

    public function setDescription(string $description): self
    {
        $this->description = $description;

        return $this;
    }

    public function getUser()
    {
        return $this->user;
    }

    public function setUser($user)
    {
        $this->user = $user;

        return $this;
    }

    public function getScore(): ?int
    {
        return $this->score;
    }

    public function setScore(?int $score): self
    {
        $this->score = $score;

        return $this;
    }

    public function getVisible(): ?bool
    {
        return $this->visible;
    }

    public function setVisible(bool $visible): self
    {
        $this->visible = $visible;

        return $this;
    }

    public function getCategories()
    {
        return $this->categories;
    }

    public function setCategories($categories)
    {
        $this->categories = $categories;

        return $this;
    }

    public function __toString()
    {
        return $this->getTitle();
    }
}

Category.php 实体

<?php

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
{
    public function __construct() {

        $this->recipe = new \Doctrine\Common\Collections\ArrayCollection();

    }

    /**
     * @ORM\Id
     * @ORM\GeneratedValue
     * @ORM\Column(type="integer")
     */
    private $id;

    /** 
     * @ORM\Column(type="string", length=255, nullable=false)
     */
    private $name;
    
    /**
     * @ORM\ManyToMany(targetEntity="Recipe", mappedBy="categories")
     */
    private $recipe;

    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;
    }

    public function getRecipe()
    {
        return $this->recipe;
    }

    public function setRecipe()
    {
        return $this->recipe;
    }

    public function addRecipe(Recipe $recipe): self
    {
        if (!$this->recipe->contains($recipe)) {
            $this->recipe[] = $recipe;
            $recipe->addCategory($this);
        }

        return $this;
    }

    public function removeRecipe(Recipe $recipe): self
    {
        if ($this->recipe->removeElement($recipe)) {
            $recipe->removeCategory($this);
        }

        return $this;
    }
}

【问题讨论】:

  • 尝试在Recipe构造函数中初始化$categories
  • 我做到了。您可以在 Recipe 构造函数中看到它,但它没有运行

标签: symfony doctrine-orm doctrine symfony-forms


【解决方案1】:

如果您需要将 EntityType 设置为具有多个选项的选择,则必须使用 multipleexpanded 选项。

这就是为什么它实际上只返回一个对象。

查看有关EntityType 的文档 https://symfony.com/doc/4.4/reference/forms/types/entity.html#basic-usage

对你来说应该是这样的:

->add('categories',EntityType::class,[
    'class' => Category::class,
    'mapped' => true,
    'label' => 'Categoria',
    'choice_label' => 'name',
    'multiple' => true,
    'expanded' => false
])

看看这张表: https://symfony.com/doc/4.4/reference/forms/types/entity.html#select-tag-checkboxes-or-radio-buttons

编辑!

我切换了多个和扩展值!当然应该是这样的:

Element Type                            Expanded    Multiple
select tag (with multiple attribute)    false       true

我的错!

【讨论】:

  • 当我这样做时出现此错误:必须管理传递给选择字段的“Doctrine\Common\Collections\ArrayCollection”类型的实体。也许您忘记将其持久化在实体管理器中?
  • 您应该尝试在您的Recipe 实体上添加cascade={"persist"}。像这样:@ORM\ManyToMany(targetEntity="Category", inversedBy="recipe", cascade={"persist"})
  • 我这样做了,但它不能解决这个错误。这很奇怪,因为多个和扩展运行之间的所有组合。但是multiple = false和expanded = true得到这个错误
  • @Cristina 你说得对,我的回答有误,我同时切换了多个和扩展!
  • 我更喜欢这种 HTML 格式:w3schools.com/tags/tryit.asp?filename=tryhtml_select 为什么扩展和多个不能为假?我更喜欢这种 HTML 格式,但它不起作用
猜你喜欢
  • 1970-01-01
  • 2019-03-04
  • 1970-01-01
  • 1970-01-01
  • 2021-01-15
  • 2020-01-06
  • 2019-12-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多