我尝试为名为 Product 的实体创建一个表单,其中包含来自实体条码的嵌入表单。当我尝试转到表单添加一个产品时,出现消息“App\Entity\Product::getBarcodes() 的返回值必须实现接口 Doctrine\Common\Collections\Collection,返回空值”。我在 __construct 中说初始化条形码以实现 Collection 但仍然相同..
我的条码实体
<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass="App\Repository\BarcodeRepository")
*/
class Barcode
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
*/
private $code;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Product", inversedBy="barcodes")
* @ORM\JoinColumn(nullable=false)
*/
private $product;
public function __construct(Product $product = null)
{
$this->product = $product;
}
public function getId(): ?int
{
return $this->id;
}
public function getCode(): ?string
{
return $this->code;
}
public function setCode(string $code): self
{
$this->code = $code;
return $this;
}
public function getProduct(): ?Product
{
return $this->product;
}
public function setProduct(?Product $product): self
{
$this->product = $product;
return $this;
}
}
和我的产品类型:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name')
->add('slug')
->add('picture')
->add('barcodes', CollectionType::class, [
'entry_type' => BarcodeType::class,
'allow_add' => true,
'allow_delete' => true,
'prototype' => true,
'by_reference' => false
])
->add('is_activated')
->add('comments')
;
}
四季花海