Fields of 2 entities in one CrudController, in EasyAdmin 3

806 views Asked by At

I am working on a personal project and I have a little problem.

I have 2 entities, Bibliotheque and ComicBook. I would like the fields of these 2 entities to be present in a single CrudController, which I named BibliothequeCrudController.

I implemented the createIndexQueryBuilder function to modify the results of my list.

public function createIndexQueryBuilder(SearchDto $searchDto, EntityDto $entityDto, FieldCollection $fields, FilterCollection $filters): QueryBuilder
    {

        $em = $this->getDoctrine()->getManager();
        $qb = $em->createQueryBuilder();
        $qb->select(array('b'));
        $qb->from('App\Entity\Bibliotheque', 'b');
        $qb->innerJoin('b.comic_book', 'c');
        $qb->addSelect('c');
        // $qb->Where("b.user = ".$this->security->getUser()->getId());
        // dd($qb->getQuery()->getResult());
        return $qb;
    }

The function works well. If I use :

dd($qb->getQuery()->getResult());

I get the information :

BibliothequeCrudController.php on line 73:
array:1 [▼
  0 => App\Entity\Bibliotheque {#1145 ▼
    -id: 64
    -comic_book: Doctrine\ORM\PersistentCollection {#1149 ▼
      -snapshot: array:1 [ …1]
      -owner: App\Entity\Bibliotheque {#1145}
      -association: array:20 [ …20]
      -em: Doctrine\ORM\EntityManager {#704 …11}
      -backRefFieldName: "bibliothequesComicBook"
      -typeClass: Doctrine\ORM\Mapping\ClassMetadata {#1083 …}
      -isDirty: false
      #collection: Doctrine\Common\Collections\ArrayCollection {#1148 ▼
        -elements: array:1 [▼
          0 => App\Entity\ComicBook {#1143 ▼
            -id: 4
            -titre: Proxies\__CG__\App\Entity\Serie {#1194 ▶}
            -sous_titre: "(Must Have)"
            -date_creation: DateTime @1593561600 {#1147 ▶}
            -isbn: null
            -nb_page: 200
            -resume: null
            -image: "609b8fcbea932677061888.jpg"
            -imageFile: null
            -genre: Doctrine\ORM\PersistentCollection {#1203 ▶}
            -edition: Doctrine\ORM\PersistentCollection {#1229 ▶}
            -publication: Doctrine\ORM\PersistentCollection {#1251 ▶}
            -scenario: Doctrine\ORM\PersistentCollection {#1283 ▶}
            -dessin: Doctrine\ORM\PersistentCollection {#1315 ▶}
            -couleur: Doctrine\ORM\PersistentCollection {#1347 ▶}
            -numero: null
            -bibliothequesComicBook: Doctrine\ORM\PersistentCollection {#1350 ▶}
            -episode: null
          }
        ]
      }
      #initialized: true
    }
    -user: App\Entity\User {#855 ▶}
  }
]

but I can't seem to get the information in a field. I tried with a :

TextField::new('ComicBook.sous_titre'),

and with other ways, but I'm stuck.

I provide 2 entities ​:

// App\Entity\Bibliotheque.php

class Bibliotheque
{
    /**
     * @ORM\Id
     * @ORM\GeneratedValue
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\ManyToMany(targetEntity=ComicBook::class, inversedBy="bibliothequesComicBook")
     */
    private $comic_book;

    /**
     * @ORM\ManyToOne(targetEntity=User::class, inversedBy="bibliothequeUser")
     */
    private $user;
// App\Entity\ComicBook.php

class ComicBook
{
    /**
     * @ORM\Id
     * @ORM\GeneratedValue
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\ManyToOne(targetEntity=Serie::class, inversedBy="serieComics")
     * @ORM\JoinColumn(nullable=false)
     */
    private $titre;

    /**
     * @ORM\Column(type="string", length=255, nullable=true)
     */
    private $sous_titre;

    /**
     * @ORM\Column(type="date", nullable=true)
     */
    private $date_creation;

    /**
     * @ORM\Column(type="bigint", nullable=true)
     */
    private $isbn;

    /**
     * @ORM\Column(type="integer", nullable=true)
     */
    private $nb_page;

    /**
     * @ORM\Column(type="text", nullable=true)
     */
    private $resume;

    /**
     * @ORM\Column(type="string", length=255, nullable=true)
     * @var string
     */
    private $image;

    /**
     * @Vich\UploadableField(mapping="images", fileNameProperty="image")
     * @var File
     */
    private $imageFile;

    /**
     * @ORM\ManyToMany(targetEntity=Genre::class, inversedBy="genreComics")
     */
    private $genre;

    /**
     * @ORM\ManyToMany(targetEntity=Edition::class, inversedBy="editionComics")
     */
    private $edition;

    /**
     * @ORM\ManyToMany(targetEntity=Publication::class, inversedBy="publicationComics")
     */
    private $publication;

    /**
     * @ORM\ManyToMany(targetEntity=Scenariste::class, inversedBy="scenarioComics")
     */
    private $scenario;

    /**
     * @ORM\ManyToMany(targetEntity=Dessinateur::class, inversedBy="dessinComics")
     */
    private $dessin;

    /**
     * @ORM\ManyToMany(targetEntity=Coloriste::class, inversedBy="couleurComics")
     */
    private $couleur;

    /**
     * @ORM\Column(type="string", length=4, nullable=true)
     */
    private $numero;

    /**
     * @ORM\ManyToMany(targetEntity=Bibliotheque::class, mappedBy="comic_book")
     */
    private $bibliothequesComicBook;

    /**
     * @ORM\Column(type="array", nullable=true)
     */
    private $episode = [];

If you need more information, ask me.

0

There are 0 answers