你好,我是 php 和 symfony 的新手,我需要帮助!! 我正在使用 api 平台, 我有一个 invoiceLineItems 字段,它是一个 invoiceLineItems 实体数组,当调用 setInvoiceLineItems 时,它期望参数是一个对象而不是 ArrayCollection。
这是我的 invoiceLineItems 实体的示例:
<?php
namespace App\Bundle\Entity;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
use ApiPlatform\Core\Annotation\ApiResource;
use Symfony\Component\Serializer\Annotation\Groups;
use App\Bunble\Repository\InvoiceLineItemRepository;
use App\Bundle\Entity\User;
#[ORM\Entity(repositoryClass: InvoiceLineItemRepository::class)]
#[ORM\Table(name: '`invoiceLineItems`')]
#[ApiResource(
normalizationContext: ['groups' => ['read:InvoiceLineItem']],
denormalizationContext: ['groups' => ['write:InvoiceLineItem']],
collectionOperations: [
'get',
'post'
],
itemOperations: [
'get',
'put',
'delete'
],
)]
class InvoiceLineItem
{
#[ORM\Id]
#[ORM\GeneratedValue(strategy: 'AUTO')]
#[ORM\Column(type: Types::INTEGER)]
#[Groups(['read:InvoiceLineItems', 'read:Invoice'])]
private ?int $id;
#[ORM\Column(type: Types::STRING, length: 255, nullable: false)]
#[Groups(['read:InvoiceLineItem', 'write:InvoiceLineItem', 'read:Invoice'])]
private string $description;
#[ORM\Column(type: Types::STRING, length: 255, nullable: true)]
#[Groups(['read:InvoiceLineItem', 'write:InvoiceLineItem', 'read:Invoice'])]
private string $subDescription;
#[ORM\Column(type: Types::FLOAT, nullable: true)]
#[Groups(['read:InvoiceLineItem', 'write:InvoiceLineItem', 'read:Invoice'])]
private string $price;
#[ORM\Column(type: Types::FLOAT, length: 255, nullable: true)]
#[Groups(['read:InvoiceLineItem', 'write:InvoiceLineItem', 'read:Invoice'])]
private string $vatPercentage;
#[ORM\Column(type: Types::FLOAT, length: 100, nullable: true,)]
#[Groups(['read:InvoiceLineItem', 'write:InvoiceLineItem', 'read:Invoice'])]
private string $vat;
#[ORM\Column(type: Types::FLOAT, nullable: false)]
#[Groups(['read:InvoiceLineItem', 'write:InvoiceLineItem', 'read:Invoice'])]
private string $totalPrice;
#[ORM\ManyToOne(
targetEntity: Invoice::class,
inversedBy: 'invoice',
)]
#[ORM\JoinColumn(
nullable: false
)]
#[Groups(['read:InvoiceLineItem'])]
private Invoice $invoice;
public function getId(): ?int
{
return $this->id;
}
public function getDescription(): ?string
{
return $this->description;
}
public function setDescription(string $description): self
{
$this->description = $description;
return $this;
}
public function getSubDescription(): ?string
{
return $this->subDescription;
}
public function setSubDescription(string $subDescription): self
{
$this->subDescription = $subDescription;
return $this;
}
public function getPrice(): ?float
{
return $this->price;
}
public function setPrice(float $price): self
{
$this->price = $price;
return $this;
}
public function getVATPercentage(): ?float
{
return $this->vatPercentage;
}
public function setVATPercentage(float $vatPercentage): self
{
$this->vatPercentage = $vatPercentage;
return $this;
}
public function getVat(): ?float
{
return $this->vat;
}
public function setVat(float $vat): self
{
$this->vat = $vat;
return $this;
}
public function getTotalPrice(): ?float
{
return $this->totalPrice;
}
public function setTotalPrice(float $totalPrice): self
{
$this->totalPrice = $totalPrice;
return $this;
}
public function getInvoice(): ?Invoice
{
return $this->invoice;
}
}
这是我的发票实体:
<?php
namespace App\Bundle\Entity;
use Symfony\Component\Validator\Constraints as Assert;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
use ApiPlatform\Core\Annotation\ApiResource;
use DateTime;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Symfony\Component\Serializer\Annotation\Groups;
use App\Bundle\Repository\InvoiceRepository;
use App\Bundle\Entity\User;
use App\Bundle\Entity\InvoiceItems;
use App\Bundle\Entity\InvoiceLineItems;
use Symfony\Component\Serializer\Annotation\MaxDepth;
#[ORM\Entity(repositoryClass: InvoiceRepository::class)]
#[ORM\Table(name: '`invoice`')]
#[ApiResource(
normalizationContext: ['groups' => ['read:Invoice']],
denormalizationContext: ['groups' => ['write:Invoice', 'read:Invoice']],
collectionOperations: [
'get',
'post'
],
itemOperations: [
'get',
'put',
'delete'
],
)]
class Invoice
{
#[ORM\Id]
#[ORM\GeneratedValue(strategy: 'AUTO')]
#[ORM\Column(type: Types::INTEGER)]
#[Groups(['read:Invoice'])]
private ?int $id;
#[MaxDepth(100)]
#[ORM\OneToMany(targetEntity: InvoiceLineItems::class, mappedBy: 'invoice', cascade: ['persist', 'remove'])]
#[Groups(['read:Invoice', 'write:Invoice'])]
private $invoiceLineItems;
public function __construct()
{
$this->invoiceLineItems = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getInvoiceLineItems(): ?Collection
{
return $this->invoiceLineItems;
}
public function setInvoiceLineItems(Collection $invoiceLineItems): self
{
foreach ($invoiceLineItems as $lineItemData) {
$lineItem = new InvoiceLineItem();
$lineItem->setDescription($lineItemData['description'] ?? null);
$lineItem->setSubDescription($lineItemData['subDescription'] ?? null);
$lineItem->setPrice($lineItemData['price'] ?? null);
$lineItem->setVATPercentage($lineItemData['vatPercentage'] ?? null);
$lineItem->setVat($lineItemData['vat'] ?? null);
$lineItem->setTotalPrice($lineItemData['totalPrice'] ?? null);
$lineItem->setInvoice($this);
$this->invoiceLineItems->add($lineItem);
}
return $this;
}
}
我已经尝试将参数类型显式设置为 array 和 ArrayCollection 但它仍然会抛出错误