Symfony EasyAdmin Bundle one to many

Jotosha picture Jotosha · Jan 3, 2016 · Viewed 8k times · Source

Is there anyway to do one to many relations in EasyAdmin bundle in symfony2?

So far i get my User working, but not other entities with one to many relations.

I have database in doctrine with MySQL.

Answer

fcpauldiaz picture fcpauldiaz · Jan 3, 2016

All kinds of entity associations are supported by EasyAdminBundle.

There is no documentation about entity associations because it is not part of the EasyAdminBundle, but Doctrine. For example, this is a OneToMany Association.

/**
 * 
 * @var ArrayCollection
 * @ORM\OneToMany(targetEntity="DocumentBundle\Entity\Document", mappedBy="course")
 * 
 */
private $documents;

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

Here is the other side of the association

/**
 * Many-to-one relationship between documents and course
 *
 * @var ArrayCollection
 * @ORM\ManyToOne(targetEntity="CourseBundle\Entity\Course",inversedBy="documents")
 * @ORM\JoinColumn(name="course_id", referencedColumnName="id")
 */
private $course;

The configuration is simply like this:

easy_admin:
    site_name: 'Learn-In Admin'
    entities:
        Courses:
            class: CourseBundle\Entity\Course
            new:
               fields: ['name','code'] 
        Documents:
            class: DocumentBundle\Entity\Document

You can find all the examples about Association Mapping in Doctrine documentation.