Thursday, 5 September 2013

Self referencing man-to-many Usage & DQL

Self referencing man-to-many Usage & DQL

I have two tables:
category
id | name
1 | Programming
2 | Designing
3 | PHP
4 | C#
5 | Adobe Photoshop
category_tree
category_id | parent_id
1 | NULL
2 | NULL
3 | 1
4 | 1
5 | 2
Here is the Entity class:
/**
* @ORM\Entity
* @ORM\Table(name="category")
*/
class Category
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
protected $id;
/**
* @ORM\Column(type="text", name="name")
*/
protected $name;
/**
* @ORM\Column(type="text", name="description")
*/
/**
* @ORM\ManyToMany(targetEntity="Category")
* @ORM\JoinTable(name="category_tree",
* joinColumns={@ORM\JoinColumn(name="category_id",
referencedColumnName="id")},
* inverseJoinColumns={@ORM\JoinColumn(name="parent_id",
referencedColumnName="id")}
* )
*/
protected $categories;
public function __construct()
{
$this->categories = new \Doctrine\Common\Collections\ArrayCollection();
}
public function addCategory(Category $category)
{
$this->categories[] = $category;
return $this;
}
public function getCategories()
{
return $this->categories;
}
public function getId()
{
return $this->id;
}
public function setName($name)
{
$this->name = $name;
return $this;
}
public function getName()
{
return $this->name;
}
}
I have these questions:
Is this Entity fine w.r.t Table Relationship?
How can i join both tables ?
How can i use this Entity to insert values in both tables?

No comments:

Post a Comment