2

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.

Jotosha
  • 183
  • 3
  • 13

1 Answers1

10

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.

Javier Eguiluz
  • 3,987
  • 2
  • 23
  • 44
fcpauldiaz
  • 423
  • 6
  • 19
  • Can you show me how to accomplish that with some example? I can't find anything about this in documentation(or im missing it :/) – Jotosha Jan 03 '16 at 18:27
  • Thank you so much, i hope this will solve my problem. Gonna check it out tomorrow morning :) – Jotosha Jan 03 '16 at 19:28
  • Everything seem to work now, except that i had to convert to string ID's from other entities(one-to-many). public function __toString() { return (string) $this->id; } And now it display only ID, and i cannot remove or edit those values.. – Jotosha Jan 05 '16 at 15:44
  • What do you want to show in the __toString() method? You can show any attribute, not only the id of the entity. – fcpauldiaz Jan 05 '16 at 16:39
  • Well, the problem is i have two one-to-many relations, one storing id, other one types. http://imgur.com/FOc9FKM I would like to display the types, is there anyway to achieve that? – Jotosha Jan 06 '16 at 20:43
  • Do you have two one-to-many relations to the same class? I think you should open another question to add more details. – fcpauldiaz Jan 08 '16 at 04:10