You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PHP Symfony API Platform下InvoiceLineItems集合赋值报错求助

问题修复方案

1. 实体关联注解错误

  • Invoice实体的OneToMany注解中,targetEntity写错了实体类名,实际应为InvoiceLineItem而非InvoiceLineItems,修正代码:
#[MaxDepth(100)]
#[ORM\OneToMany(targetEntity: InvoiceLineItem::class, mappedBy: 'invoice', cascade: ['persist', 'remove'])]
#[Groups(['read:Invoice', 'write:Invoice'])]
private $invoiceLineItems;
  • InvoiceLineItem实体的ManyToOne注解中,inversedBy指向的属性名错误,Invoice里的关联属性是invoiceLineItems而非invoice,修正代码:
#[ORM\ManyToOne(
    targetEntity: Invoice::class,
    inversedBy: 'invoiceLineItems',
)]
#[ORM\JoinColumn(nullable: false)]
#[Groups(['read:InvoiceLineItem'])]
private Invoice $invoice;

2. setInvoiceLineItems方法逻辑错误

API Platform反序列化时,会自动将请求的JSON数组转为InvoiceLineItem对象集合,你当前的方法把对象当成数组访问,导致cannot use object of type Entity as array错误。修改方法如下:

public function setInvoiceLineItems(Collection $invoiceLineItems): self
{
    // 先清空现有集合,避免重复数据
    $this->invoiceLineItems->clear();
    
    foreach ($invoiceLineItems as $lineItem) {
        // 直接为对象设置关联
        $lineItem->setInvoice($this);
        $this->invoiceLineItems->add($lineItem);
    }

    return $this;
}

如果需要兼容手动传入数组的场景,可将参数类型改为iterable,同时增加对象判断:

public function setInvoiceLineItems(iterable $invoiceLineItems): self
{
    $this->invoiceLineItems->clear();
    
    foreach ($invoiceLineItems as $lineItem) {
        if (!$lineItem instanceof InvoiceLineItem) {
            $item = new InvoiceLineItem();
            $item->setDescription($lineItem['description'] ?? null);
            $item->setSubDescription($lineItem['subDescription'] ?? null);
            $item->setPrice($lineItem['price'] ?? null);
            $item->setVATPercentage($lineItem['vatPercentage'] ?? null);
            $item->setVat($lineItem['vat'] ?? null);
            $item->setTotalPrice($lineItem['totalPrice'] ?? null);
            $item->setInvoice($this);
            $this->invoiceLineItems->add($item);
            continue;
        }
        $lineItem->setInvoice($this);
        $this->invoiceLineItems->add($lineItem);
    }

    return $this;
}

3. InvoiceLineItem字段类型声明错误

实体中price、vatPercentage等字段的数据库类型是FLOAT,但属性声明为string,会导致类型不匹配,修正为对应浮点类型:

#[ORM\Column(type: Types::FLOAT, nullable: true)]
#[Groups(['read:InvoiceLineItem', 'write:InvoiceLineItem', 'read:Invoice'])]
private ?float $price;

#[ORM\Column(type: Types::FLOAT, length: 255, nullable: true)]
#[Groups(['read:InvoiceLineItem', 'write:InvoiceLineItem', 'read:Invoice'])]
private ?float $vatPercentage;

#[ORM\Column(type: Types::FLOAT, length: 100, nullable: true)]
#[Groups(['read:InvoiceLineItem', 'write:InvoiceLineItem', 'read:Invoice'])]
private ?float $vat;

#[ORM\Column(type: Types::FLOAT, nullable: false)]
#[Groups(['read:InvoiceLineItem', 'write:InvoiceLineItem', 'read:Invoice'])]
private float $totalPrice;

4. 补充InvoiceLineItem的setInvoice方法

当前InvoiceLineItem实体缺少设置关联的方法,无法绑定Invoice对象,需添加:

public function setInvoice(Invoice $invoice): self
{
    $this->invoice = $invoice;
    return $this;
}

内容的提问来源于stack exchange,提问作者Success Nduka

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.24 21:38:08