Skip to content
Advertisement

Symfony 5 – ManyToOne __isInitialized__: false

I try to get value from object but objects return __isInitialized__: false and null values

what did I miss?

twig situ dump :

AppEntitySitu {#915 ▼
  -id: 142
  ...

  -event: Proxies__CG__AppEntityEvent {#941 ▼
    +__isInitialized__: true
    -id: 1
    -title: "XXX"
    -userId: 3
    -validated: true
    -lang: AppEntityLang {#711 ▶}
    -categories: DoctrineORMPersistentCollection {#1201 ▶}
    #situs: DoctrineORMPersistentCollection {#1230 ▶}
     …2
  }
  -categoryLevel1: Proxies__CG__AppEntityCategory {#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__AppEntityCategory {#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
  }

situ entity :

use AppEntityEvent;
use AppEntityCategory;

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

    ...

    /**
     * @ORMManyToOne(targetEntity="AppEntityEvent", inversedBy="situs")
     */
    private $event;

    /**
     * @ORMManyToOne(targetEntity="AppEntityCategory", inversedBy="situs1")
     */
    private $categoryLevel1;

    /**
     * @ORMManyToOne(targetEntity="AppEntityCategory", 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;
    }

event entity :

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

    ...

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

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

category entity :

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

    ...

    /**
     * @ORMManyToOne(targetEntity="AppEntityEvent", inversedBy="categories")
     */
    private $event;
    
    /**
     * @ORMColumn(type="integer", nullable=true)
     */
    private $parentId;

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

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

Advertisement

Answer

You just got to know the doctrine lazy loading feature. It fetches the data until you fired the getter method the first time. You could know this because you see the “Proxy-Entities” with allowes doctrine to archive the lazy load behavior. To change this you can edit the fetch behavior in your entities. For example this would activate it for the event property. Please note the ",fetch="EAGER"" part which is modified.

/**
 * @ORMManyToOne(targetEntity="AppEntityEvent", inversedBy="situs", fetch="EAGER")
 */
private $event;
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement