symfony querybuilder查找跨其他实体的对象



我在symfony中有以下实体:

<?php
namespace AppEntity;
use AppRepositoryCartaRepository;
use DateInterval;
use DateTime;
use DoctrineCommonCollectionsArrayCollection;
use DoctrineCommonCollectionsCollection;
use DoctrineORMMapping as ORM;
/**
* @ORMEntity(repositoryClass=CartaRepository::class)
* @ORMTable(name="sac_sala_cartas")
*/
class Carta
{
/**
* @ORMId
* @ORMGeneratedValue
* @ORMColumn(type="integer")
*/
private $id;
/**
* @ORMManyToOne(targetEntity=Cliente::class, inversedBy="cartas")
* @ORMJoinColumn(nullable=false)
*/
private $cliente;
/**
* @ORMColumn(type="string", length=255)
*/
private $documento;
/**
* @ORMColumn(type="smallint")
*/
private $estado;
/**
* @ORMColumn(type="text", nullable=true)
*/
private $observaciones;
/**
* @ORMColumn(type="date")
*/
private $fecha_solicitud;
/**
* @ORMColumn(type="date")
*/
private $fecha_respuesta;
/**
* @ORMColumn(type="date")
*/
private $fecha_vencimiento;
/**
* @ORMManyToMany(targetEntity=Fondo::class, inversedBy="cartas")
* @ORMJoinTable(name="sac_sala_cartas_fondos")
*/
private $fondos;
public function __construct()
{
$this->fondos = new ArrayCollection();
$this->estado = 0;
$this->fecha_solicitud = new DateTime('now');
$this->fecha_respuesta = DATE_ADD(new DateTime('now'), DateInterval::createFromDateString('3 day'));
$this->fecha_vencimiento = DATE_ADD(new DateTime('now'), DateInterval::createFromDateString('1 year'));
}
public function getId(): ?int
{
return $this->id;
}
public function getCliente(): ?Cliente
{
return $this->cliente;
}
public function setCliente(?Cliente $cliente): self
{
$this->cliente = $cliente;
return $this;
}
public function getDocumento(): ?string
{
return $this->documento;
}
public function setDocumento(string $documento): self
{
$this->documento = $documento;
return $this;
}
public function getEstado(): ?int
{
return $this->estado;
}
public function setEstado(int $estado): self
{
$this->estado = $estado;
return $this;
}
public function getObservaciones(): ?string
{
return $this->observaciones;
}
public function setObservaciones(string $observaciones): self
{
$this->observaciones = $observaciones;
return $this;
}
public function getFechaSolicitud(): ?DateTimeInterface
{
return $this->fecha_solicitud;
}
public function setFechaSolicitud(DateTimeInterface $fecha_solicitud): self
{
$this->fecha_solicitud = $fecha_solicitud;
return $this;
}
public function getFechaRespuesta(): ?DateTimeInterface
{
return $this->fecha_respuesta;
}
public function setFechaRespuesta(DateTimeInterface $fecha_respuesta): self
{
$this->fecha_respuesta = $fecha_respuesta;
return $this;
}
public function getFechaVencimiento(): ?DateTimeInterface
{
return $this->fecha_vencimiento;
}
public function setFechaVencimiento(DateTimeInterface $fecha_vencimiento): self
{
$this->fecha_vencimiento = $fecha_vencimiento;
return $this;
}
/**
* @return Collection<int, Fondo>
*/
public function getFondos(): Collection
{
return $this->fondos;
}
public function addFondo(Fondo $fondo): self
{
if (!$this->fondos->contains($fondo)) {
$this->fondos[] = $fondo;
}
return $this;
}
public function removeFondo(Fondo $fondo): self
{
$this->fondos->removeElement($fondo);
return $this;
}
}

我需要找到所有的"客户"。从"carta"存储库的"carta"有"estado = 1"。

到目前为止,我找到了所有的"estado=1">

$cartas = $this->em->getRepository(Carta::class);
$cartasAutorizadas = $cartas->createQueryBuilder("c")
->where("c.estado = :carta_estado")
->setParameter("carta_estado", 1)
->getQuery()
->getResult()
;

,我知道有一种方法使用连接或类似的东西,但坦率地说,我的知识很差…希望有人能帮助我。

如果需要更多的代码,请不要犹豫,问。BR…

也许我的问题表达不正确,但经过一番阅读,我分享了下面正确的句子:

$clientes = $this->em->getRepository(Cliente::class);
$clientesAuth = $clientes->createQueryBuilder('cliente')
->leftJoin('cliente.cartas', 'c')
->where('c.estado = :carta_estado')
->setParameter("carta_estado", 1)
->getQuery()
->getResult();

我希望我的回答对以后的查询有帮助

Try

$cartas = $this->em->getRepository(Carta::class);
$cartasAutorizadas = $cartas->createQueryBuilder('c')
->select('cl')
->innerJoin('c.cliente', 'cl')
->where('c.estado = :carta_estado')
->setParameter('carta_estado', 1)
->distinct()
->getQuery()
->getResult()
;

最新更新