Symfony/Doctrine Notice: Undefined index: 000*

I’m hope your are doing fine !

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 AppEntity;

use AppRepositoryUserCartSavedRepository;
use DateTime;
use DoctrineCommonCollectionsArrayCollection;
use DoctrineCommonCollectionsCollection;
use DoctrineORMMapping as ORM;
use SymfonyComponentValidatorConstraintsDate;

/**
 * @ORMEntity(repositoryClass=UserCartSavedRepository::class)
 */
class UserCartSaved
{
    /**
     * @ORMId
     * @ORMGeneratedValue
     * @ORMColumn(type="integer")
     */
    private $id;

    /**
     * @ORMManyToOne(targetEntity=User::class, inversedBy="userCartSaveds")
     * @ORMJoinColumn(nullable=false)
     */
    private $user;

    /**
     * @ORMOneToMany(targetEntity=UserCartSavedProducts::class, mappedBy="userCartSaved")
     */
    private $userCartSavedProducts;

    /**
     * @ORMColumn(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 AppEntity;

use AppRepositoryUserCartSavedProductsRepository;
use DoctrineORMMapping as ORM;

/**
 * @ORMEntity(repositoryClass=UserCartSavedProductsRepository::class)
 */
class UserCartSavedProducts
{
    /**
     * @ORMId
     * @ORMGeneratedValue
     * @ORMColumn(type="integer")
     */
    private $id;

    /**
     * @ORMManyToOne(targetEntity=UserCartSaved::class, inversedBy="userCartSavedProducts")
     * @ORMJoinColumn(nullable=false)
     */
    private $userCartSaved;

    /**
     * @ORMManyToOne(targetEntity=Product::class, inversedBy="userCartSavedProducts", cascade={"persist"})
     * @ORMJoinColumn(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 AppEntity;

use AppRepositoryProductRepository;
use DoctrineCommonCollectionsArrayCollection;
use DoctrineCommonCollectionsCollection;
use DoctrineORMMapping as ORM;

/**
 * @ORMEntity(repositoryClass=ProductRepository::class)
 */
class Product
{
    /**
     * @ORMId()
     * @ORMGeneratedValue()
     * @ORMColumn(type="integer")
     */
    private $id;

    /**
     * @ORMOneToMany(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;
    }
}

Thank by advance for your help 😉

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

Your email address will not be published. Required fields are marked *