Barif
Barif

Reputation: 1552

Relations in one table

I have simple Company table with relations:

id    name        parentId
1     Company1    0
2     Company2    1
3     Company3    1 
4     Company4    3

How i can write Doctrine annotation which relate entries in this table?

Upvotes: 3

Views: 61

Answers (1)

Florent
Florent

Reputation: 826

Seems your Entities are only Company, each Company have a name and is related to another Company with a ManyToOne relation.

Your Company entity should looks something like :

/**
 * Company
 *
 * @ORM\Table()
 * @ORM\Entity
 */
class Company {
    /**
     * @var integer
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @var string
     * @ORM\Column(name="name", type="string", length=100)
     */
    protected $name;

    /**
     * @ORM\ManyToOne(targetEntity="Company")
     * @ORM\JoinColumn(name="parentId", referencedColumnName="id")
     */
    protected $parent;
}

For the base class, you should consider using php app/console doctrine:generate:entity that will do all the work for you. You'll just have to add the parent relation manually.

You should have a look at Symfony's book on this topic : http://symfony.com/doc/current/book/doctrine.html.

All association mapping configuration is explain in Doctrine's docs : http://docs.doctrine-project.org/en/2.0.x/reference/association-mapping.html

Upvotes: 5

Related Questions