pas possible de faire un inner join avec le query builder
Bonsoir,
je tente de faire un querybuilder qui contient un innerjoin, mais je n'arrive pas à aller jusqu'au bout !
une table animals,
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
|
class Animals
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
* @Assert\NotBlank(message="vous devez rentrer un nom commun")
*/
private $commonName;
/**
* @ORM\ManyToMany(targetEntity=Continents::class, inversedBy="animals")
* @Assert\Count(min="1", minMessage="vous devez rentrer au moins 1 continent")
* @ORM\JoinColumn(nullable=false)
*
*/
private $continents;
public function __construct()
{
$this->population = new ArrayCollection();
$this->continents = new ArrayCollection();
}
/**
* @return Collection|Continents[]
*/
public function getContinents(): Collection
{
return $this->continents;
}
public function addContinent(Continents $continent): self
{
if (!$this->continents->contains($continent)) {
$this->continents[] = $continent;
}
return $this;
}
public function removeContinent(Continents $continent): self
{
$this->continents->removeElement($continent);
return $this;
}
} |
une table continents,
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
|
class Continents
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*
*/
private $continentsName;
/**
* @ORM\ManyToMany(targetEntity=Animals::class, mappedBy="continents")
*/
private $animals;
public function __construct()
{
$this->animals = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getContinentsName(): ?string
{
return $this->continentsName;
}
public function setContinentsName(?string $continentsName): self
{
$this->continentsName = $continentsName;
return $this;
}
/**
* @return Collection|Animals[]
*/
public function getAnimals(): Collection
{
return $this->animals;
}
public function addAnimals(Animals $animals): self
{
if (!$this->animals->contains($animals)) {
$this->animals[] = $animals;
$animals->addContinent($this);
}
return $this;
}
public function removeAnimal(Animals $animal): self
{
if ($this->animals->removeElement($animal)) {
$animal->removeContinent($this);
}
return $this;
}
} |
une relation many to many et la requête:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
class AnimalsRepository extends ServiceEntityRepository
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, Animals::class);
}
public function findAnimals($species, $diets): array
{
$qb = $this->createQueryBuilder('a')
->innerJoin('a.continents', 'c', 'WITH', ?? les id de la table animals = les id animals de la table d'association animals_continents)
->Where("c.id=1");
$query = $qb->getQuery();
return $query->execute();
}
} |
Après , symfony me crée une table animals_continents dans ma BDD, mais je n'ai pas d'entité animals_continents ( pas crée automatiquement ).
Est-ce que c'est ça le problème ? ou c'est moi qui ne comprend pas la doc ( il y des chances ! )
Merci pour vos réponses, c'est du classique, mais j'avoue ne pas comprendre la doc de Doctrine
Laurent.