我嘗試為名為 Product 的實體創建一個表單,其中包含來自實體條碼的嵌入表單。當我嘗試轉到表單添加一個產品時,出現消息“App\Entity\Product::getBarcodes() 的返回值必須實現接口 Doctrine\Common\Collections\Collection,返回空值”。我在 __construct 中說初始化條形碼以實現 Collection 但仍然相同..我的條碼實體<?phpnamespace 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') ; }
“簡單” CollectionType 包含錯誤
慕婉清6462132
2021-09-18 16:07:34