亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

“簡單” CollectionType 包含錯誤

“簡單” CollectionType 包含錯誤

PHP
慕婉清6462132 2021-09-18 16:07:34
我嘗試為名為 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')        ;    }
查看完整描述

1 回答

?
四季花海

TA貢獻1811條經驗 獲得超5個贊

在您的Product實體中,null通過更改Collection為允許返回類型的可能性?Collection:


/**

 * @return Collection|Barcode[]|null

 */

public function getBarcodes(): ?Collection

{

    return $this->barcodes;

}


查看完整回答
反對 回復 2021-09-18
  • 1 回答
  • 0 關注
  • 138 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號