Etienne
Etienne

Reputation: 420

How to use Gedmo nested tree for storing multiple trees in one single table?

I have a table which store a tree for each username. My entity looks like this:

/**
* Confsaves
* @Gedmo\Tree(type="nested")
* @ORM\Table(name="confsaves")
 *@ORM\Entity(repositoryClass="Gedmo\Tree\Entity\Repository\NestedTreeRepository")
 */
class Confsaves
{
/**
 * @var string
 *
 * @ORM\Column(name="Name", type="string", length=200, nullable=true)
 */
private $name;

/**
 * @var string
 * 
 * @Gedmo\TreeRoot
 * @ORM\Column(name="Username", type="string", length=50, nullable=true)
 */
private $username;

/**
 * @Gedmo\TreeParent
 * @ORM\ManyToOne(targetEntity="Confsaves", inversedBy="children")
 * @ORM\JoinColumn(name="parent_id", referencedColumnName="id", onDelete="CASCADE", nullable=true)
 */
private $parent;

/**
 * @ORM\OneToMany(targetEntity="Confsaves", mappedBy="parent")
 * @ORM\OrderBy({"lft" = "ASC"})
 */
private $children;

/**
 * @var integer
 * 
 * @Gedmo\TreeLeft
 * @ORM\Column(name="lft", type="integer", nullable=true)
 */
private $lft;

/**
 * @var integer
 *
 * @Gedmo\TreeRight
 * @ORM\Column(name="rght", type="integer", nullable=true)
 */
private $rght;


/**
 * @var integer
 *
 * @ORM\Column(name="id", type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="IDENTITY")
 */
private $id;

I want build a different tree for each user.

In my controller I have created the repository like this:

$em = $this->getDoctrine()->getManager();
$repo = $em->getRepository('MyBundle:Confsaves');

How can I set the scope of the repository only on the user connected? Is level necessary for build tree function?

I use an existing database that only have left, right and parent arguments.

Upvotes: 0

Views: 1238

Answers (1)

Has been a long time, but for the record:

If you want a tree for each user, you must establish a relationship between your Confsaves and User entities, more specifically, between the root property of your Confsaves entity and your User one. Doctrine Extensions supports relationships on the root property of your tree since 2.4. Make sure you are using the right version. Cost me a day of debugging.

So, to relate your nested set tree to a user entity, just perform a ManyToOne in your User, and the inverse relationship in your Tree entity.

Upvotes: 2

Related Questions