Doctrine: Many To Many prázdna kolekcia

orion05
Člen | 9
+
0
-

Ahojte,

prečítal som si už všetko možné, ako to riešiť, no stále mám s problém so vzťahom ManyToMany.

Mám dve entity, diary a tag, keď pridávam nový tag a dumpnem si upravovanú entitu diary tak vidím kolekciu obsahujúcu pole s novým tag-om. Do databázy a aj do join tabulky sa tieto dáta uložia. Keď ale chcem niekde inde na stránke zobraziť tú istú entitu diary (aj po refreshnutí), jej parameter s tagmi je rovnako kolekcia ale prázdna.

Prikladám kódy:
Tags.php

<?php

namespace App;

use Doctrine\ORM\Mapping as ORM;

/**
 * All entity classes must be declared as such.
 *
 * @ORM\Entity
 * @ORM\Table(name="tags")
 */
class Tags extends \Kdyby\Doctrine\Entities\BaseEntity{

	.
	.
	.

    /**
     * @ORM\ManyToMany(targetEntity="Diary", mappedBy="diaries", fetch="EXTRA_LAZY",cascade={"persist"})
     */
    public $diaries;

	public function __construct() {
        $this->diaries = new \Doctrine\Common\Collections\ArrayCollection();
    }

    public function addDiaries(Diary $diary)
    {
        $this->diary->add($diary);
        $diary->addTag($this);
        return $this;
    }

    public function getDiaries(){
        return $this->diary;
    }

    public function removeDiary($diary){
        $this->diary->removeElement($diary);
    }

}

Diary.php

<?php

namespace App;

use Doctrine\ORM\Mapping as ORM;

/**
 * All entity classes must be declared as such.
 *
 * @ORM\Entity
 * @ORM\Table(name="diary")
 */
class Diary extends \Kdyby\Doctrine\Entities\BaseEntity{

   	.
	.
	.

    /**
	 * @ORM\ManyToMany(targetEntity="Tags", inversedBy="diaries",cascade={"persist"})
	 * @ORM\JoinTable(
	 *     name="diaries_tags",
	 *     joinColumns={
	 *         @ORM\JoinColumn(name="diary_id", referencedColumnName="id")
	 *     },
	 *     inverseJoinColumns={
	 *         @ORM\JoinColumn(name="tag_id", referencedColumnName="id")
	 *     }
	 * )
	 */
    public $tags;

	public function __construct() {
        $this->tags = new \Doctrine\Common\Collections\ArrayCollection();
    }

    public function addTag(Tags $tag)
    {
        $this->tags->add($tag);
	    $tag->addDiary($this);

	   return $this;
    }

    public function getTags(){
        return $this->tags;
    }

    public function removeTag(Tags $tag)
    {
        $this->tags->removeElement($tag);
    }

}

pridavanie tagu do diary – kolekcia ok

$tag = new \App\Tags();
$tag->name = $values->tags;

$diary->addTag($tag);
$this->em->merge($diary);
$this->em->flush();
dump($diary);

zobrazenie diary – prazdna kolekcia

$this->diary = $this->em->find(\App\Diary::getClassName(), $id);
dump($this->diary);

rovnako prazdna kolekcia je aj pri:

dump($this->diary->tags);

očakával by som, že po načítaní konkrétnej entity Diary sa mi v kolekcii zobrazia všetky Tags, ktoré sú spojené s denníkom, tak ako sú uložené v join tabuľke

MajklNajt
Člen | 471
+
0
-

Ahoj, máš tam typo – na začiatku si definuješ property $diaries, ale v obslužných metódach voláš $this->diary – malo by tam byť $this->diaries

MajklNajt
Člen | 471
+
0
-

a ešte je prúser aj tu:

/**
 * @ORM\ManyToMany(targetEntity="Diary", mappedBy="diaries", fetch="EXTRA_LAZY",cascade={"persist"})
 */
public $diaries;

správne by si mal mať mappedBy=„tags“

orion05
Člen | 9
+
0
-

ďakujem, opravil som, prejde to aj orm validátorom, no môj problém s prázdnou kolekciou po refreshi zostáva