如何让图像依赖于一对多关系中的文章?
how to get the image depend an article in a relation one to many?
- 我正在寻找获取横幅图片
- 每个横幅都有很多图像,具有 OneToMany
关系
- 我想我只有 select 横幅
where isActive = true
和图像集,但不幸的是没有 return 图像
此查询在 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 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;
}
}
图片实体:
<?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;
}
}
问题已解决
public function activeBanner()
{
return
$this->createQueryBuilder('image')
->leftJoin('image.banner', 'banner')
->andWhere('banner.isActive = :isActive')
->setParameter('isActive', true)
->getQuery()
->execute();
}
这是正确的 queryBuilder,它应该在图像存储库中,希望我的解决方案能帮助其他堆栈开发人员
- 我正在寻找获取横幅图片
- 每个横幅都有很多图像,具有 OneToMany 关系
- 我想我只有 select 横幅
where isActive = true
和图像集,但不幸的是没有 return 图像
此查询在 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 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;
}
}
图片实体:
<?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;
}
}
问题已解决
public function activeBanner()
{
return
$this->createQueryBuilder('image')
->leftJoin('image.banner', 'banner')
->andWhere('banner.isActive = :isActive')
->setParameter('isActive', true)
->getQuery()
->execute();
}
这是正确的 queryBuilder,它应该在图像存储库中,希望我的解决方案能帮助其他堆栈开发人员