传递给 ArrayCollection::__construct() 的参数 1 必须是给定的数组对象类型
Argument 1 passed to ArrayCollection::__construct() must be of the type array object given
我正在用 Symfony 4.4 编程。
我有一个类别为 属性 的 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;
}
}
如果您需要 EntityType 是具有多个选择的 select,则必须使用 multiple
和 expanded
选项。
这就是为什么它实际上 return 只有一个对象。
查看有关 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
])
看看这个 table:
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
我错了!
我正在用 Symfony 4.4 编程。
我有一个类别为 属性 的 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;
}
}
如果您需要 EntityType 是具有多个选择的 select,则必须使用 multiple
和 expanded
选项。
这就是为什么它实际上 return 只有一个对象。
查看有关 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
])
看看这个 table: 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
我错了!