title: Parent Child Relationship With A Single Entity In Doctrine 2
body: <p>I have database table that looks like this:</p>
  
  <pre>
  +----+--------+--------------------+
  | id | parent | description        |
  +----+--------+--------------------+
  |  1 | null   | P Cat 1            |
  |  2 | 1      | Child 1 of P Cat 1 |
  |  3 | 1      | Child 2 of P Cat 1 |
  |  4 | null   | P Cat 2            |
  |  5 | 4      | Child 1 of P Cat 2 |
  |  6 | 4      | Child 2 of P Cat 2 |
  +----+--------+--------------------+
  </pre>
  
  <p>How can I create a doctrine 2 entity that has these columns, but I need the parent column to reference the "id" column as a parent. Of course, a parent record has a null "parent" column value.</p>
  
  <p>So fair I have</p>
  
  <pre><code>&lt;?php
  namespace MyNamespace;
  use Doctrine\ORM\Mapping AS ORM;
  use Doctrine\Common\Collections\ArrayCollection;
  /**
   * @ORM\Entity
   * @ORM\Table(name="category")
   **/
  class Category
  {
      /**
       * @ORM\Id
       * @ORM\Column(type="integer", name="id")
       * @ORM\GeneratedValue
       */
      protected $id;
  
      /**
       * Creates a parent / child relationship on this entity.
       *
       * @ORM\ManyToOne(targetEntity="MyNamespace\Category",inversedBy="id")
       * @ORM\JoinColumn(name="FK_parent_id", referencedColumnName="id", nullable=true)
       */
      protected $parent = null;
  
      /**
       * @ORM\Column(type="string", name="description", length=250)
       *
       * @var string
       */
      protected $description;
  
      /**
       * Gets the Primary key value.
       *
       * @return integer
       */
      public function getId()
      {
          return $this-&gt;id;
      }
  
      /**
       * Sets another category ID as the parent of this category.
       */
      public function setParent(Category $category)
      {
          $this-&gt;parent = $category;
      }
  
      /**
       * Clears the parent id and makes it null.
       */
      public function clearParent()
      {
          $this-&gt;parent = null;
      }
  
      /**
       * Sets the description.
       *
       * @param string $description
       * @return Category
       */
      public function setDescription($description)
      {
          $this-&gt;description = $description;
          return $this;
      }
  
      /**
       * Gets the description value.
       *
       * @return string
       */
      public function getDescription()
      {
          return $this-&gt;description;
      }
  }
  </code></pre>
  
  <p>Needless to say, this doesn't appear to work. The questions are:</p>
  
  <ol>
  <li>The setParent() method doesn't appear to work as expected when another entity is added as a parent.</li>
  <li>I need a getChildren() method on this entity. How can I achieve that?</li>
  </ol>