Symfony 5 - ManyToOne __isInitialized__: false

Symfony 5 - ManyToOne __isInitialized__: false

我尝试从对象中获取值,但是对象 return __isInitialized__: false 和空值

我错过了什么?

树枝原地倾倒:

App\Entity\Situ {#915 ▼
  -id: 142
  ...

  -event: Proxies\__CG__\App\Entity\Event {#941 ▼
    +__isInitialized__: true
    -id: 1
    -title: "XXX"
    -userId: 3
    -validated: true
    -lang: App\Entity\Lang {#711 ▶}
    -categories: Doctrine\ORM\PersistentCollection {#1201 ▶}
    #situs: Doctrine\ORM\PersistentCollection {#1230 ▶}
     …2
  }
  -categoryLevel1: Proxies\__CG__\App\Entity\Category {#959 ▼
    +__isInitialized__: false
    -id: 17
    -title: null
    -description: null
    -dateCreation: null
    -userId: null
    -validated: null
    -lang: null
    -event: null
    -parentId: null
    -situs1: null
    -situs2: null
     …2
  }
  -categoryLevel2: Proxies\__CG__\App\Entity\Category {#958 ▼
    +__isInitialized__: false
    -id: 18
    -title: null
    -description: null
    -dateCreation: null
    -userId: null
    -validated: null
    -lang: null
    -event: null
    -parentId: null
    -situs1: null
    -situs2: null
     …2
  }

现场实体:

use App\Entity\Event;
use App\Entity\Category;

/**
 * @ORM\Entity(repositoryClass=SituRepository::class)
 */
class Situ
{    
    /**
     * @ORM\Id
     * @ORM\GeneratedValue
     * @ORM\Column(type="integer")
     */
    private $id;

    ...

    /**
     * @ORM\ManyToOne(targetEntity="App\Entity\Event", inversedBy="situs")
     */
    private $event;

    /**
     * @ORM\ManyToOne(targetEntity="App\Entity\Category", inversedBy="situs1")
     */
    private $categoryLevel1;

    /**
     * @ORM\ManyToOne(targetEntity="App\Entity\Category", inversedBy="situs2")
     */
    private $categoryLevel2;

    ...

    public function __toString(): ?string
    {
        return $this->event;
        return $this->categoryLevel1;
        return $this->categoryLevel2;
    }

    ...

    public function getEvent(): ?Event
    {
        return $this->event;
    }

    public function setEvent(?Event $event): self
    {
        $this->event = $event;

        return $this;
    }

    public function getCategoryLevel1(): ?Category
    {
        return $this->categoryLevel1;
    }

    public function setCategoryLevel1(?Category $categoryLevel1): self
    {
        $this->categoryLevel1 = $categoryLevel1;

        return $this;
    }

    public function getCategoryLevel2(): ?Category
    {
        return $this->categoryLevel2;
    }

    public function setCategoryLevel2(?Category $categoryLevel2): self
    {
        $this->categoryLevel2 = $categoryLevel2;

        return $this;
    }

事件实体:


/**
 * @ORM\Entity(repositoryClass=EventRepository::class)
 */
class Event
{
    /**
     * @ORM\Id
     * @ORM\GeneratedValue
     * @ORM\Column(type="integer")
     */
    private $id;

    ...

    /**
     * @ORM\OneToMany(targetEntity=Category::class, mappedBy="event")
     */
    private $categories;

    /**
    * @ORM\OneToMany(targetEntity=Situ::class, cascade={"persist"}, mappedBy="event")
    */
    protected $situs;

    public function __construct()
    {
        $this->categories = new ArrayCollection();
        $this->situs = new ArrayCollection();
    }

    ...

    /**
     * @return Collection|Category[]
     */
    public function getCategories(): Collection
    {
        return $this->categories;
    }

    public function addCategory(Category $category): self
    {
        if (!$this->categories->contains($category)) {
            $this->categories[] = $category;
            $category->setEvent($this);
        }

        return $this;
    }

    public function removeCategory(Category $category): self
    {
        if ($this->categories->removeElement($category)) {
            // set the owning side to null (unless already changed)
            if ($category->getEvent() === $this) {
                $category->setEvent(null);
            }
        }

        return $this;
    }

    /**
     * @return Collection|Situ[]
     */
    public function getSitus(): ?Collection
    {
        return $this->situs;
    }
     
    public function addSitu(Situ $situ): self
    {
        if (!$this->situs->contains($situ)) {
            $this->situs[] = $situ;
            $situ->setEvent($this);
        }
        
        return $this;
    }

    public function removeSitu(Situ $situ): self
    {
        if ($this->situs->contains($situ)) {
            $this->situs->removeElement($situ);
            // set the owning side to null (unless already changed)
            if ($situ->getEvent() === $this) {
                $situ->setEvent(null);
            }
        }
        return $this;
    }

类别实体:


/**
 * @ORM\Entity(repositoryClass=CategoryRepository::class)
 */
class Category
{
    /**
     * @ORM\Id
     * @ORM\GeneratedValue
     * @ORM\Column(type="integer")
     */
    private $id;

    ...

    /**
     * @ORM\ManyToOne(targetEntity="App\Entity\Event", inversedBy="categories")
     */
    private $event;
    
    /**
     * @ORM\Column(type="integer", nullable=true)
     */
    private $parentId;

    /**
     * @ORM\OneToMany(targetEntity=Situ::class, cascade={"persist"}, mappedBy="categoryLevel1")
     */
    private $situs1;

    /**
     * @ORM\OneToMany(targetEntity=Situ::class, cascade={"persist"}, mappedBy="categoryLevel2")
     */
    private $situs2;

    public function __construct()
    {
        $this->situs1 = new ArrayCollection();
        $this->situs2 = new ArrayCollection();
    }

    ...

    public function getEvent(): ?int
    {
        return $this->event;
    }

    public function setEvent(?Event $event): self
    {
        $this->event = $event;

        return $this;
    }

    public function getParentId(): ?int
    {
        return $this->parentId;
    }

    public function setParentId(int $parent): self
    {
        $this->parentId = $parentId;

        return $this;
    }

您刚刚了解了学说延迟加载功能。它会获取数据,直到您第一次触发 getter 方法。您可能知道这一点,因为您看到带有允许原则的“Proxy-Entities”来存档延迟加载行为。要更改此设置,您可以编辑实体中的提取行为。例如,这将为事件 属性 激活它。请注意修改的",fetch="EAGER""部分。

/**
 * @ORM\ManyToOne(targetEntity="App\Entity\Event", inversedBy="situs", fetch="EAGER")
 */
private $event;