Bonjour à tous,
Sur mon projet Symfony je souhaite afficher des données rassemblant 3 entités avec des conditions particulières. Les 3 entités sont Country(pays), Category(categorie) et Aircraft(avion). Je souhaite obtenir un résultat ressemblant à ceci :
Pays 1 (si des avions lui sont attachés sinon on ne l'affiche pas) => requête simple
Mon but est de connaitre la facon la plus logique et efficiente de coder ca. J'espère avoir été assez clair :)
Je vous partage le code des 3 entités (uniquement ce qui est nécessaire)
Entité Country :
class Country
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(name="name", type="string", length=255, unique=true)
*/
private $name;
/**
* @ORM\OneToMany(targetEntity=Aircraft::class, mappedBy="country")
*/
private $aircraft;
public function __construct()
{
$this->aircraft = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getName(): ?string
{
return $this->name;
}
/**
* @return Collection|Aircraft[]
*/
public function getAircraft(): Collection
{
return $this->aircraft;
}
}
Entité Categorie :
class AircraftCategory
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
*/
private $name;
/**
* @ORM\OneToMany(targetEntity=Aircraft::class, mappedBy="category")
*/
private $aircraft;
public function __construct()
{
$this->aircraft = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getName(): ?string
{
return $this->name;
}
/**
* @return Collection|Aircraft[]
*/
public function getAircraft(): Collection
{
return $this->aircraft;
}
}
Entité Aircraft :
class Aircraft
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
*/
private $name;
/**
* @ORM\ManyToOne(targetEntity=Country::class, inversedBy="aircraft")
* @ORM\JoinColumn(nullable=false)
*/
private $country;
/**
* @ORM\ManyToOne(targetEntity=AircraftCategory::class, inversedBy="aircraft")
* @ORM\JoinColumn(nullable=false)
*/
private $category;
public function getId(): ?int
{
return $this->id;
}
public function getName(): ?string
{
return $this->name;
}
public function getCountry(): ?Country
{
return $this->country;
}
public function getCategory(): ?AircraftCategory
{
return $this->category;
}
}
Merci à vous pour vos suggestions.
Bonjour,
Si c'est juste pour de l'affichage tu peut faire ça en créant une function dans ton CountryRepository.
@aka-aka Hum je ne suis pas sur que cela va m'aider ou alors je n'ai pas saisi le sens de ta réponse.
Le but est d'obtenir le résultat souhaité avec le traitement le plus efficace possible (nombre de requêtes, functions ? ... )
voila mon idée :
Controller
/**
* @Route("/aircraft", name="aircraft")
*/
public function index(AircraftRepository $aircraftRepository): Response
{
$aircraft = $aircraftRepository->findByCountryAndCategory();
return $this->render('aircraft/index.html.twig', [
'aircraft' => $aircraft,
]);
}
Repository
// /**
// * @return Aircraft[] Returns an array of Aircraft objects
// */
public function findByCountryAndCategory ()
{
return $this->createQueryBuilder('a')
->addSelect('c')
->addSelect('ac')
->join('a.country', 'c')
->leftJoin('a.category', 'ac')
->getQuery()
->getResult();
}
Vue
{% block body %}
<h1>Aircraft</h1>
{% for item in aircraft %}
<h2>Country : {{ item.country.name }}</h2>
{% for aircraft in item.country.aircraft %}
<p> Aircraft ==> [{{ aircraft.name }}] Category ==> [{{ aircraft.category.name}}] </p>
{% endfor %}
{% endfor %}
{% endblock %}
Resultat
<h1>Aircraft</h1>
<h2>Country : Colombie</h2>
<p> Aircraft ==> [aircraft_1] Category ==> [ATR 42] </p>
<p> Aircraft ==> [aircraft_7] Category ==> [Airbus A318] </p>
<p> Aircraft ==> [aircraft_13] Category ==> [Airbus A310] </p>
<h2>Country : Croatie</h2>
<p> Aircraft ==> [aircraft_2] Category ==> [ATR 42] </p>
<p> Aircraft ==> [aircraft_8] Category ==> [Airbus A318] </p>
<p> Aircraft ==> [aircraft_14] Category ==> [Airbus A310] </p>
<h2>Country : Cuba</h2>
<p> Aircraft ==> [aircraft_3] Category ==> [ATR 42] </p>
<p> Aircraft ==> [aircraft_9] Category ==> [Airbus A318] </p>
<p> Aircraft ==> [aircraft_15] Category ==> [Airbus A220] </p>
<h2>Country : Danemark</h2>
<p> Aircraft ==> [aircraft_4] Category ==> [ATR 42] </p>
<p> Aircraft ==> [aircraft_10] Category ==> [Airbus A318] </p>
<p> Aircraft ==> [aircraft_16] Category ==> [Airbus A220] </p>
<h2>Country : Djibouti</h2>
<p> Aircraft ==> [aircraft_5] Category ==> [ATR 42] </p>
<p> Aircraft ==> [aircraft_11] Category ==> [Airbus A310] </p>
<p> Aircraft ==> [aircraft_17] Category ==> [Airbus A220] </p>
<h2>Country : Dominique</h2>
<p> Aircraft ==> [aircraft_6] Category ==> [Airbus A318] </p>
<p> Aircraft ==> [aircraft_12] Category ==> [Airbus A310] </p>
<p> Aircraft ==> [aircraft_18] Category ==> [Airbus A220] </p>
<h2>Country : Colombie</h2>
<p> Aircraft ==> [aircraft_1] Category ==> [ATR 42] </p>
<p> Aircraft ==> [aircraft_7] Category ==> [Airbus A318] </p>
<p> Aircraft ==> [aircraft_13] Category ==> [Airbus A310] </p>
<h2>Country : Croatie</h2>
<p> Aircraft ==> [aircraft_2] Category ==> [ATR 42] </p>
<p> Aircraft ==> [aircraft_8] Category ==> [Airbus A318] </p>
<p> Aircraft ==> [aircraft_14] Category ==> [Airbus A310] </p>
<h2>Country : Cuba</h2>
<p> Aircraft ==> [aircraft_3] Category ==> [ATR 42] </p>
<p> Aircraft ==> [aircraft_9] Category ==> [Airbus A318] </p>
<p> Aircraft ==> [aircraft_15] Category ==> [Airbus A220] </p>
<h2>Country : Danemark</h2>
<p> Aircraft ==> [aircraft_4] Category ==> [ATR 42] </p>
<p> Aircraft ==> [aircraft_10] Category ==> [Airbus A318] </p>
<p> Aircraft ==> [aircraft_16] Category ==> [Airbus A220] </p>
<h2>Country : Djibouti</h2>
<p> Aircraft ==> [aircraft_5] Category ==> [ATR 42] </p>
<p> Aircraft ==> [aircraft_11] Category ==> [Airbus A310] </p>
<p> Aircraft ==> [aircraft_17] Category ==> [Airbus A220] </p>
<h2>Country : Dominique</h2>
<p> Aircraft ==> [aircraft_6] Category ==> [Airbus A318] </p>
<p> Aircraft ==> [aircraft_12] Category ==> [Airbus A310] </p>
<p> Aircraft ==> [aircraft_18] Category ==> [Airbus A220] </p>
<h2>Country : Colombie</h2>
<p> Aircraft ==> [aircraft_1] Category ==> [ATR 42] </p>
<p> Aircraft ==> [aircraft_7] Category ==> [Airbus A318] </p>
<p> Aircraft ==> [aircraft_13] Category ==> [Airbus A310] </p>
<h2>Country : Croatie</h2>
<p> Aircraft ==> [aircraft_2] Category ==> [ATR 42] </p>
<p> Aircraft ==> [aircraft_8] Category ==> [Airbus A318] </p>
<p> Aircraft ==> [aircraft_14] Category ==> [Airbus A310] </p>
<h2>Country : Cuba</h2>
<p> Aircraft ==> [aircraft_3] Category ==> [ATR 42] </p>
<p> Aircraft ==> [aircraft_9] Category ==> [Airbus A318] </p>
<p> Aircraft ==> [aircraft_15] Category ==> [Airbus A220] </p>
<h2>Country : Danemark</h2>
<p> Aircraft ==> [aircraft_4] Category ==> [ATR 42] </p>
<p> Aircraft ==> [aircraft_10] Category ==> [Airbus A318] </p>
<p> Aircraft ==> [aircraft_16] Category ==> [Airbus A220] </p>
<h2>Country : Djibouti</h2>
<p> Aircraft ==> [aircraft_5] Category ==> [ATR 42] </p>
<p> Aircraft ==> [aircraft_11] Category ==> [Airbus A310] </p>
<p> Aircraft ==> [aircraft_17] Category ==> [Airbus A220] </p>
<h2>Country : Dominique</h2>
<p> Aircraft ==> [aircraft_6] Category ==> [Airbus A318] </p>
<p> Aircraft ==> [aircraft_12] Category ==> [Airbus A310] </p>
<p> Aircraft ==> [aircraft_18] Category ==> [Airbus A220] </p>