首页 > 解决方案 > 传递给 ArrayCollection::__construct() 的参数 1 必须是给定的数组对象类型

问题描述

我在 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 行

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

UploadRecipesType 表单

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

标签: symfonydoctrine-ormdoctrinesymfony-forms

解决方案


如果您需要 EntityType 成为具有多个选择的选择,则必须使用multipleand options 。expanded

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

查看有关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

我的错 !


推荐阅读