Duplicate entry ‘1’ for key Symfony

when I’m trying create new entry I’ve got that informations(symfony):

An exception occurred while executing a query: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry ‘1’ for key ‘UNIQ_ED896F46AF89CCED’
This error I have when in db is something actually, so first entries are doing good.
I’m using mariadb. Symfony version is 6+.
Controller Order:

<?php

namespace AppController;

use AppEntityOrderDetail;
use AppEntityProduct;
use DoctrineORMEntityManagerInterface;
use SymfonyBundleFrameworkBundleControllerAbstractController;
use SymfonyComponentHttpFoundationJsonResponse;
use SymfonyComponentRoutingAttributeRoute;
use SymfonyComponentHttpFoundationRequest;
use AppEntityOrder;

class OrderController extends AbstractController
{
    #[Route('/order', name: 'create_order', methods: ['POST'])]
    public function createOrder(Request $request, EntityManagerInterface $em): JsonResponse
    {
        $data = json_decode($request->getContent(), true);

        if (empty($data))
            return new JsonResponse(['error' => 'Error Processing Request'], 404);

        $order = new Order();
        $order->setCreateat(new DateTime());

        $total = 0;
        foreach ($data as $item) {
            $product = $em->getRepository(Product::class)->find($item['id']);

            if (!$product) {
                return new JsonResponse(['error' => 'Product not found'], 404);
            }

            $orderDetail = new OrderDetail();
            $orderDetail->setProductid($product);
            $orderDetail->setQuantity($item['quantity']);
            $orderDetail->setPrice($product->getPrice() * $item['quantity']);
            $orderDetail->setOrderid($order);

            $total += $orderDetail->getPrice();

            $order->addOrderDetail($orderDetail);
            $em->persist($orderDetail);
        }
        $order->setTotal($total);
        $order->setUpdateat(new DateTime());
        $order->setStatus(Order::STATUS_NEW);
        $em->persist($order);
        $em->flush();

        return new JsonResponse($order, 200);
    }
}

Order entity:

<?php

namespace AppEntity;

use AppRepositoryOrderRepository;
use DoctrineCommonCollectionsArrayCollection;
use DoctrineCommonCollectionsCollection;
use DoctrineDBALTypesTypes;
use DoctrineORMMapping as ORM;

#[ORMEntity(repositoryClass: OrderRepository::class)]
#[ORMTable(name: '`order`')]
class Order
{
    #[ORMId]
    #[ORMGeneratedValue]
    #[ORMColumn]
    private ?int $id = null;

    #[ORMColumn(type: Types::DECIMAL, precision: 10, scale: 0)]
    private ?string $total = null;

    #[ORMColumn(type: Types::DATETIME_MUTABLE)]
    private ?DateTimeInterface $createat = null;

    #[ORMColumn(type: Types::DATETIME_MUTABLE)]
    private ?DateTimeInterface $updateat = null;

    #[ORMColumn]
    private ?int $status = null;

    /**
     * @var Collection<int, OrderDetail>
     */
    #[ORMOneToMany(targetEntity: OrderDetail::class, mappedBy: 'orderid')]
    private Collection $productid;

    /**
     * @var Collection<int, OrderDetail>
     */
    #[ORMOneToMany(targetEntity: OrderDetail::class, mappedBy: 'orderid')]
    private Collection $orderDetails;

    public const STATUS_NEW = 0;

    public function __construct()
    {
        $this->productid = new ArrayCollection();
        $this->orderDetails = new ArrayCollection();
    }

    public function getId(): ?int
    {
        return $this->id;
    }

    public function getTotal(): ?string
    {
        return $this->total;
    }

    public function setTotal(string $total): static
    {
        $this->total = $total;

        return $this;
    }

    public function getCreateat(): ?DateTimeInterface
    {
        return $this->createat;
    }

    public function setCreateat(DateTimeInterface $createat): static
    {
        $this->createat = $createat;

        return $this;
    }

    public function getUpdateat(): ?DateTimeInterface
    {
        return $this->updateat;
    }

    public function setUpdateat(DateTimeInterface $updateat): static
    {
        $this->updateat = $updateat;

        return $this;
    }

    public function getStatus(): ?int
    {
        return $this->status;
    }

    public function setStatus(int $status): static
    {
        $this->status = $status;

        return $this;
    }

    /**
     * @return Collection<int, OrderDetail>
     */
    public function getProductid(): Collection
    {
        return $this->productid;
    }

    public function addProductid(OrderDetail $productid): static
    {
        if (!$this->productid->contains($productid)) {
            $this->productid->add($productid);
            $productid->setOrderid($this);
        }

        return $this;
    }

    public function removeProductid(OrderDetail $productid): static
    {
        if ($this->productid->removeElement($productid)) {
            // set the owning side to null (unless already changed)
            if ($productid->getOrderid() === $this) {
                $productid->setOrderid(null);
            }
        }

        return $this;
    }

    /**
     * @return Collection<int, OrderDetail>
     */
    public function getOrderDetails(): Collection
    {
        return $this->orderDetails;
    }

    public function addOrderDetail(OrderDetail $orderDetail): static
    {
        if (!$this->orderDetails->contains($orderDetail)) {
            $this->orderDetails->add($orderDetail);
            $orderDetail->setOrderid($this);
        }

        return $this;
    }

    public function removeOrderDetail(OrderDetail $orderDetail): static
    {
        if ($this->orderDetails->removeElement($orderDetail)) {
            // set the owning side to null (unless already changed)
            if ($orderDetail->getOrderid() === $this) {
                $orderDetail->setOrderid(null);
            }
        }

        return $this;
    }
}

OrderDetail entity:

<?php

namespace AppEntity;

use AppRepositoryOrderDetailRepository;
use DoctrineDBALTypesTypes;
use DoctrineORMMapping as ORM;

#[ORMEntity(repositoryClass: OrderDetailRepository::class)]
class OrderDetail
{
    #[ORMId]
    #[ORMGeneratedValue]
    #[ORMColumn]
    private ?int $id = null;

    #[ORMManyToOne(inversedBy: 'orderDetails')]
    private ?Order $orderid = null;

    #[ORMOneToOne(cascade: ['persist', 'remove'])]
    private ?Product $productid = null;

    #[ORMColumn]
    private ?int $quantity = null;

    #[ORMColumn(type: Types::DECIMAL, precision: 10, scale: 0)]
    private ?string $price = null;

    public function getId(): ?int
    {
        return $this->id;
    }

    public function getOrderid(): ?Order
    {
        return $this->orderid;
    }

    public function setOrderid(?Order $orderid): static
    {
        $this->orderid = $orderid;

        return $this;
    }

    public function getProductid(): ?Product
    {
        return $this->productid;
    }

    public function setProductid(?Product $productid): static
    {
        $this->productid = $productid;

        return $this;
    }

    public function getQuantity(): ?int
    {
        return $this->quantity;
    }

    public function setQuantity(int $quantity): static
    {
        $this->quantity = $quantity;

        return $this;
    }

    public function getPrice(): ?string
    {
        return $this->price;
    }

    public function setPrice(string $price): static
    {
        $this->price = $price;

        return $this;
    }
}

Guys any suggest? May I can’t see something? Maybe this relation is problem.

New contributor

bratniak is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật