Reputation: 69
I have a problem about the flush on 2 linked entities (ManyToOne, OneToMany).
I'm on Symfony 5.1.
I just want to persist one "UserSavedCard" with many "UserCartSavedProducts" entities.
But I have an error when I flushed my entities and this error come to this file "in vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php (line 3013)"
My function that throw the error :
/**
* Save current user cart in database for later
* @param string|null $title
*/
public function saveCart(?string $title)
{
$cart = $this->getCart();
$cartSaved = new UserCartSaved();
$cartSaved->setUser($this->security->getUser());
$this->em->persist($cartSaved);
foreach ($cart as $item) {
$savedProduct = new UserCartSavedProducts();
$savedProduct->setProduct($item['product']);
$savedProduct->setUserCartSaved($cartSaved);
$this->em->persist($savedProduct);
}
$this->em->flush();
}
When I execute this code above But I have this error :
Notice: Undefined index: 000000007e86ae93000000003f3a2fbb
There is my entites :
UserCartSaved:
<?php
namespace App\Entity;
use App\Repository\UserCartSavedRepository;
use DateTime;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints\Date;
/**
* @ORM\Entity(repositoryClass=UserCartSavedRepository::class)
*/
class UserCartSaved
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\ManyToOne(targetEntity=User::class, inversedBy="userCartSaveds")
* @ORM\JoinColumn(nullable=false)
*/
private $user;
/**
* @ORM\OneToMany(targetEntity=UserCartSavedProducts::class, mappedBy="userCartSaved")
*/
private $userCartSavedProducts;
/**
* @ORM\Column(type="datetime")
*/
private $createdAt;
public function __construct()
{
$this->userCartSavedProducts = new ArrayCollection();
$this->createdAt = new DateTime();
}
public function getId(): ?int
{
return $this->id;
}
public function getUser(): ?User
{
return $this->user;
}
public function setUser(?User $user): self
{
$this->user = $user;
return $this;
}
/**
* @return Collection|UserCartSavedProducts[]
*/
public function getUserCartSavedProducts(): Collection
{
return $this->userCartSavedProducts;
}
public function addUserCartSavedProduct(UserCartSavedProducts $userCartSavedProduct): self
{
if (!$this->userCartSavedProducts->contains($userCartSavedProduct)) {
$this->userCartSavedProducts[] = $userCartSavedProduct;
$userCartSavedProduct->setUserCartSaved($this);
}
return $this;
}
public function removeUserCartSavedProduct(UserCartSavedProducts $userCartSavedProduct): self
{
if ($this->userCartSavedProducts->contains($userCartSavedProduct)) {
$this->userCartSavedProducts->removeElement($userCartSavedProduct);
// set the owning side to null (unless already changed)
if ($userCartSavedProduct->getUserCartSaved() === $this) {
$userCartSavedProduct->setUserCartSaved(null);
}
}
return $this;
}
public function getCreatedAt(): ?\DateTimeInterface
{
return $this->createdAt;
}
public function setCreatedAt(\DateTimeInterface $createdAt): self
{
$this->createdAt = $createdAt;
return $this;
}
}
UserCartSavedProducts :
<?php
namespace App\Entity;
use App\Repository\UserCartSavedProductsRepository;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass=UserCartSavedProductsRepository::class)
*/
class UserCartSavedProducts
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\ManyToOne(targetEntity=UserCartSaved::class, inversedBy="userCartSavedProducts")
* @ORM\JoinColumn(nullable=false)
*/
private $userCartSaved;
/**
* @ORM\ManyToOne(targetEntity=Product::class, inversedBy="userCartSavedProducts", cascade={"persist"})
* @ORM\JoinColumn(nullable=false)
*/
private $product;
public function getId(): ?int
{
return $this->id;
}
public function getUserCartSaved(): ?UserCartSaved
{
return $this->userCartSaved;
}
public function setUserCartSaved(?UserCartSaved $userCartSaved): self
{
$this->userCartSaved = $userCartSaved;
return $this;
}
public function getProduct(): ?Product
{
return $this->product;
}
public function setProduct(?Product $product): self
{
$this->product = $product;
return $this;
}
}
Product
<?php
namespace App\Entity;
use App\Repository\ProductRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass=ProductRepository::class)
*/
class Product
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\OneToMany(targetEntity=UserCartSavedProducts::class, mappedBy="product")
*/
private $userCartSavedProducts;
public function __construct()
{
$this->userCartSavedProducts = new ArrayCollection();
}
/**
* @return Collection|UserCartSavedProducts[]
*/
public function getUserCartSavedProducts(): Collection
{
return $this->userCartSavedProducts;
}
public function addUserCartSavedProduct(UserCartSavedProducts $userCartSavedProduct): self
{
if (!$this->userCartSavedProducts->contains($userCartSavedProduct)) {
$this->userCartSavedProducts[] = $userCartSavedProduct;
$userCartSavedProduct->setProduct($this);
}
return $this;
}
public function removeUserCartSavedProduct(UserCartSavedProducts $userCartSavedProduct): self
{
if ($this->userCartSavedProducts->contains($userCartSavedProduct)) {
$this->userCartSavedProducts->removeElement($userCartSavedProduct);
// set the owning side to null (unless already changed)
if ($userCartSavedProduct->getProduct() === $this) {
$userCartSavedProduct->setProduct(null);
}
}
return $this;
}
}
Upvotes: 1
Views: 1810
Reputation: 129
I've run into an issue getting the same error, and in my case is that I was storing the entity object into a session and trying to flush it "as is" after retrieval. Regardless I could retrieve and dump the object and everything seemed fine (until flush), I was told that the EM lost management of the contents. So, to get management back, I had to retrieve every single id of every component of the stored object via its own getter, and then via find, retrieve them all and set them again into a new entity instance.
Upvotes: 0