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.
bratniak is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.