如何在一对多的关系中获得依赖文章的图像


  1. 我正在寻找获取横幅的图像
  2. 每个横幅都有许多与OneToMany相关的图像
  3. 我想我刚刚选择了横幅where isActive = true并获得了图像集合,但不幸的是没有返回图像

这个查询在SQL中可以工作,但我被如何使用QueryBuilder:使其工作所困扰

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

代码:

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

横幅实体:

<?php
namespace AppEntity;
use AppRepositoryBannerRepository;
use DoctrineCommonCollectionsArrayCollection;
use DoctrineCommonCollectionsCollection;
use DoctrineORMMapping as ORM;
/**
* @ORMEntity(repositoryClass=BannerRepository::class)
*/
class Banner
{
/**
* @ORMId
* @ORMGeneratedValue
* @ORMColumn(type="integer")
*/
private $id;
/**
* @ORMColumn(type="string", length=255)
*/
private $name;
/**
* @ORMColumn(type="string", length=255, nullable=true)
*/
private $description;
/**
* @ORMOneToMany(targetEntity=Image::class, mappedBy="banner", orphanRemoval=true, cascade={"persist","remove"})
*/
private $images;
/**
* @ORMColumn(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;
}
}

图像实体:

<?php
namespace AppEntity;
use AppRepositoryImageRepository;
use DoctrineORMMapping as ORM;
/**
* @ORMEntity(repositoryClass=ImageRepository::class)
*/
class Image
{
/**
* @ORMId
* @ORMGeneratedValue
* @ORMColumn(type="integer")
*/
private $id;
/**
* @ORMColumn(type="string", length=255)
*/
private $name;
/**
* @ORMManyToOne(targetEntity=Banner::class, inversedBy="images")
* @ORMJoinColumn(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;
}
}

问题已修复

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

这是正确的queryBuilder,它应该在图像库中,希望我的解决方案能帮助其他堆栈开发人员

相关内容

  • 没有找到相关文章

最新更新