Using the Doctrine QueryBuilder, I want to execute a query which in native SQL looks like this:
`SELECT image FROM image i INNER JOIN about_images a ON i.id = a.image_id`;
The result in DQL is as follows:
ImageRepository.php:
return $this->createQueryBuilder('i')
->select('i')
->innerJoin('about_images', 'a', 'WITH', 'i.id = a.imageId')
->getQuery()
->getResult();
Where image
is an entity, and about_images
is a join table (the result of a @ManyToMany
relationship). However, I get the error that about_images
is not defined, which makes sense as it is not managed by doctrine.
AboutPage.php (i.e the entity where the join table is created)
/**
* @var Image[]|ArrayCollection
*
* @ORM\ManyToMany(targetEntity="App\Entity\Image", cascade={"persist", "remove"})
* @ORM\JoinTable(name="about_images",
* joinColumns={@ORM\JoinColumn(name="about_id", referencedColumnName="id")},
* inverseJoinColumns={@ORM\JoinColumn(name="image_id", referencedColumnName="id", unique=true)})
*/
private $images;
Fields from Image entity:
/**
* @var int
*
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
/**
* @var string
*
* @ORM\Column(type="string", length=255)
*/
private $image;
/**
* @var File
*
* @Vich\UploadableField(mapping="collection_images", fileNameProperty="image")
* @Assert\File(maxSize="150M", mimeTypes={"image/jpeg", "image/jpg", "image/png", "image/gif"},
* mimeTypesMessage="The type ({{ type }}) is invalid. Allowed image types are {{ types }}")
*/
private $imageFile;
/**
* @var string
*
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $imageAlt;
/**
* @var DateTime
*
* @ORM\Column(type="datetime")
*/
private $updatedAt;
/**
* @var string
*
* @ORM\Column(type="string", nullable=true)
*/
private $alt;
How can I solve this problem? The results should be Image
entities.
Image
entity. Thank you. – Helenesh$entityManager->getRepository(AboutPage::class)->createQueryBuilder("a")->join("a.images", "i")
– SilvioQaboutPage
property in myImage
entity. The images are referenced via the join table, which is why I need to access that join table. – Helenesh$entityManager->getRepository(AboutPage::class)->createQueryBuilder("a")->join("a.images", "i")->select("a,i")
– SilvioQ