В symfony У меня есть ManyToOne
отношение:
Each Article has One Category.
Each Category may have Many Article.
Вот мои две сущности:
Артикул
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="text")
*/
private $title;
/**
* @ORM\Column(type="text")
*/
private $content;
/**
* @ORM\Column(type="datetime")
*/
private $createdAt;
/**
* @ORM\Column(type="string", nullable=true)
*/
private $image;
/**
* @ORM\Column(type="datetime", nullable=true)
*/
private $editedAt;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Category")
*/
private $category;
public function getId(): ?int
{
return $this->id;
}
public function getTitle(): ?string
{
return $this->title;
}
public function setTitle(string $title): self
{
$this->title = $title;
return $this;
}
public function getContent(): ?string
{
return $this->content;
}
public function setContent(string $content): self
{
$this->content = $content;
return $this;
}
public function getCreatedAt(): ?\DateTimeInterface
{
return $this->createdAt;
}
public function setCreatedAt(\DateTimeInterface $createdAt): self
{
$this->createdAt = $createdAt;
return $this;
}
public function getImage(): ?string
{
return $this->image;
}
public function setImage(?string $image): self
{
$this->image = $image;
return $this;
}
public function getEditedAt(): ?\DateTimeInterface
{
return $this->editedAt;
}
public function setEditedAt(?\DateTimeInterface $editedAt): self
{
$this->editedAt = $editedAt;
return $this;
}
public function getCategory(): ?Category
{
return $this->category;
}
public function setCategory(?Category $category): self
{
$this->category = $category;
return $this;
}
Категория
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
* @Assert\Unique(message="Cette catégorie existe déjà")
*/
private $name;
/**
* @return mixed
*/
public function getId()
{
return $this->id;
}
/**
* @return mixed
*/
public function getName()
{
return $this->name;
}
/**
* @param $name
* @return mixed
*/
public function setName($name)
{
return $this->name = $name;
}
У меня есть форма basi c для добавления новой категории:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name', TextType::class)
->add('submit', SubmitType::class, [
'label' => 'Add a new category'
])
;
}
Контроллер, обрабатывающий запрос:
public function addCategory(Request $request)
{
$category = new Category();
$form = $this->createForm(CategoryType::class, $category);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$category->setName($request->request->get('category_add')['name']);
$em = $this->getDoctrine()->getManager();
$em->persist($category);
$em->flush();
}
return $this->render('admin/category.html.twig', [
'form' => $form->createView(),
]);
}
Проблема в том, что когда я пытаюсь отправить форму с любым значением во Name
, она возвращает: This value should be of type array|IteratorAggregate.