how to get the image depend an article in a relation one to many?

168 views Asked by At
  1. I'm looking for get the images of a banner
  2. Every banner has many images with a relation OneToMany
  3. I think I have just select the banner where isActive = true and get the collection of images but unfortunately not return the image

This query works in SQL but I'm stuck how to make it work fin using QueryBuilder:

SELECT *
FROM image
INNER JOIN banner ON image.banner_id = banner.id
WHERE banner.is_active = true

Code:

return $this->createQueryBuilder('i') 
            ->join('i.banner', 'b', 'on', 'i.banner_id=b.id') 
            ->andWhere('b.isActive LIKE :status') 
            ->setParameter('status',true) 
            ->getQuery(); ->execute();

Banner entity:

<?php

namespace App\Entity;

use App\Repository\BannerRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity(repositoryClass=BannerRepository::class)
 */
class Banner
{
    /**
     * @ORM\Id
     * @ORM\GeneratedValue
     * @ORM\Column(type="integer")
     */
    private $id;

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

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

    /**
     * @ORM\OneToMany(targetEntity=Image::class, mappedBy="banner", orphanRemoval=true, cascade={"persist","remove"})
     */
    private $images;

    /**
     * @ORM\Column(type="boolean")
     */
    private $isActive;

    public function __construct()
    {
        $this->images = new ArrayCollection();
        $this->isActive=false;
    }

    public function getId(): ?int
    {
        return $this->id;
    }

    public function getName(): ?string
    {
        return $this->name;
    }

    public function setName(string $name): self
    {
        $this->name = $name;

        return $this;
    }

    public function getDescription(): ?string
    {
        return $this->description;
    }

    public function setDescription(?string $description): self
    {
        $this->description = $description;

        return $this;
    }

    /**
     * @return Collection|Image[]
     */
    public function getImages(): Collection
    {
        return $this->images;
    }

    public function addImage(Image $image): self
    {
        if (!$this->images->contains($image)) {
            $this->images[] = $image;
            $image->setBanner($this);
        }

        return $this;
    }

    public function removeImage(Image $image): self
    {
        if ($this->images->contains($image)) {
            $this->images->removeElement($image);
            // set the owning side to null (unless already changed)
            if ($image->getBanner() === $this) {
                $image->setBanner(null);
            }
        }

        return $this;
    }

    public function getIsActive(): ?bool
    {
        return $this->isActive;
    }

    public function setIsActive(bool $isActive): self
    {
        $this->isActive = $isActive;

        return $this;
    }
}

Image entity:

<?php

namespace App\Entity;

use App\Repository\ImageRepository;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity(repositoryClass=ImageRepository::class)
 */
class Image
{
    /**
     * @ORM\Id
     * @ORM\GeneratedValue
     * @ORM\Column(type="integer")
     */
    private $id;

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

    /**
     * @ORM\ManyToOne(targetEntity=Banner::class, inversedBy="images")
     * @ORM\JoinColumn(nullable=false)
     */
    private $banner;

    public function getId(): ?int
    {
        return $this->id;
    }

    public function getName(): ?string
    {
        return $this->name;
    }

    public function setName(string $name): self
    {
        $this->name = $name;

        return $this;
    }

    public function getBanner(): ?Banner
    {
        return $this->banner;
    }

    public function setBanner(?Banner $banner): self
    {
        $this->banner = $banner;

        return $this;
    }
}

1

There are 1 answers

0
younes-dev On BEST ANSWER

The problem is fixed

    public function activeBanner()
{
    return
        $this->createQueryBuilder('image')
            ->leftJoin('image.banner', 'banner')
            ->andWhere('banner.isActive = :isActive')
            ->setParameter('isActive', true)
            ->getQuery()
            ->execute();
}

This the right queryBuilder and it should be in image Repository, hope my solution help other stack developers