I got this error when I try to save an approvement(which has variables : products, name_client and number) to the database
A new entity was found through the relationship ‘AppEntityApprovement#products’ that was not configured to cascade persist operations for entity: telephone samsung. To solve this issue: Either explicitly call EntityManager#persist() on this unknown entity or configure cascade persist this association in the mapping for example @ManyToOne(..,cascade={“persist”}).
this is my Approvement Entity
<?php
namespace AppEntity;
use AppRepositoryApprovementRepository;
use DoctrineCommonCollectionsArrayCollection;
use DoctrineCommonCollectionsCollection;
use DoctrineORMMapping as ORM;
#[ORMEntity(repositoryClass: ApprovementRepository::class)]
class Approvement
{
#[ORMId]
#[ORMGeneratedValue]
#[ORMColumn]
private ?int $id = null;
/**
* @var Collection<int, Product>
*/
#[ORMOneToMany(targetEntity: Product::class, mappedBy: 'approvement')]
private Collection $products;
/**
* @ORMColumn(type="string", length=255, nullable=true)
*/
private ?string $name_client = null;
/**
* @ORMColumn(type="integer", nullable=true)
*/
private ?int $number = null;
public function __construct()
{
$this->products = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
/**
* @return Collection<int, Product>
*/
public function getProducts(): Collection
{
return $this->products;
}
public function addProduct(Product $product): static
{
if (!$this->products->contains($product)) {
$this->products->add($product);
$product->setApprovement($this);
}
return $this;
}
public function removeProduct(Product $product): static
{
if ($this->products->removeElement($product)) {
// set the owning side to null (unless already changed)
if ($product->getApprovement() === $this) {
$product->setApprovement(null);
}
}
return $this;
}
public function getNameClient(): ?string
{
return $this->name_client;
}
public function setNameClient(string $name_client): static
{
$this->name_client = $name_client;
return $this;
}
public function getNumber(): ?int
{
return $this->number;
}
public function setNumber(int $number): static
{
$this->number = $number;
return $this;
}
}
this is my Controller
<?php
namespace AppController;
use AppEntityApprovement;
use AppEntityCartItem;
use AppEntityProduct;
use AppFormAddApprovementType;
use AppRepositoryApprovementRepository;
use AppRepositoryProductRepository;
use DoctrineORMEntityManagerInterface;
use Proxies__CG__AppEntityPurchase;
use SymfonyBundleFrameworkBundleControllerAbstractController;
use SymfonyComponentHttpFoundationRequest;
use SymfonyComponentHttpFoundationResponse;
use SymfonyComponentHttpFoundationSessionSession;
use SymfonyComponentRoutingAttributeRoute;
use SymfonyComponentHttpFoundationSessionSessionInterface;
class ApprovementController extends AbstractController
{
#[Route('/approvements/view', name: 'view_cart')]
public function viewCart(ApprovementRepository $approvementRepository,SessionInterface $session): Response
{
$panier = $session->get('panier');
return $this->render('approvements/view.html.twig', [
'panier'=>$panier,
]);
}
#[Route('/approvements/page', name: 'approvement_page')]
public function Approvement(Request $request, EntityManagerInterface $entityManager, ProductRepository $productRepository, SessionInterface $session): Response
{
$approvement = new Approvement();
$form = $this->createForm(AddApprovementType::class, $approvement);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$products = $session->get('panier');
foreach ($products as $productValue) {
$productId = $productValue['id'];
$quantityToDecrease = $productValue['quantity'];
// Retrieve the product entity
$productEntity = $entityManager->getRepository(Product::class)->findById($productId);
$category = $productEntity->getCategory();
$description= $productEntity->getDescription();
$created=$productEntity->getCreatedAt();
$modified=$productEntity->getModifiedAt();
$imageName=$productEntity->getImageName();
if ($productEntity) {
// Decrease the quantity
$currentQuantity = $productEntity->getQte();
$newQuantity = $currentQuantity - $quantityToDecrease;
// Update the quantity in the entity
$productEntity->setQte($newQuantity);
// Persist changes
$entityManager->persist($productEntity);
$product = new Product();
$product->setQte($newQuantity);
$product->setName($productValue['name']); // Set product properties as needed
$product->setDescription($description);
$product->setCreatedAt($created);
$product->setModifiedAt($modified);
$product->setCategory($category);
$product->setImageName($imageName);
$approvement->addProduct($product);
$entityManager->persist($approvement);
}
}
$entityManager->clear($approvement);
$entityManager->flush();
$session->remove('panier');
return $this->redirectToRoute('app_approvements');
}
return $this->render('approvements/page_approvement.html.twig', [
'form' => $form->createView(),
]);
}
}
Error when I tried to save an approvement to database