簡體   English   中英

Symfony/Doctrine 通知:未定義索引:000*

[英]Symfony/Doctrine Notice: Undefined index: 000*

我對 2 個鏈接實體(ManyToOne、OneToMany)的刷新有疑問。

我在 Symfony 5.1 上。

我只想保留一個帶有許多“UserCartSavedProducts”實體的“UserSavedCard”。

但是當我刷新我的實體時出現錯誤並且這個錯誤出現在這個文件“在 vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php(第 3013 行)中”

我拋出錯誤的函數:

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

當我執行上面的代碼但我有這個錯誤:

Notice: Undefined index: 000000007e86ae93000000003f3a2fbb

有我的實體:

用戶購物車已保存:

<?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;
    }
}

產品

<?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;
    }
}

我遇到了出現相同錯誤的問題,在我的情況下,我將實體對象存儲到會話中並嘗試在檢索后“按原樣”刷新它。 不管我可以檢索和轉儲對象並且一切看起來都很好(直到刷新),我被告知 EM 失去了對內容的管理。 因此,為了重新獲得管理權,我必須通過其自己的 getter 檢索存儲對象的每個組件的每個 id,然后通過查找、檢索它們並將它們再次設置為新的實體實例。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM