Witam, próbuję rozwiązać problem podzapytania w where w doctrine i zaciąłem się na takiej sytuacji. Mam 3 pliki
1.
<?php
namespace App\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
/**
*
* @ORM\Entity(repositoryClass="App\Entity\NotificationRepository")
* @ORM\Table(name="notifications")
*/
class Notification
{
/**
* @var int
*
* @ORM\Id
* @ORM\Column(name="id", type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var ArrayCollection
*
* @ORM\ManyToMany(targetEntity="App\Entity\Service")
* @ORM\JoinTable(name="notification_services",
* joinColumns={@ORM\JoinColumn(name="notification_id", referencedColumnName="id")},
* inverseJoinColumns={@ORM\JoinColumn(name="service_id", referencedColumnName="id")}
* )
*/
private $services;
}
2.
<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
*
* @ORM\Table(name="services")
*/
class Service
{
/**
* @var int
*
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
/**
* @var bool
*
* @ORM\Column(type="boolean")
*/
private $failure;
}
3.
<?php
namespace App\Entity;
use Doctrine\ORM\QueryBuilder;
class NotificationRepository
{
/**
* @param null|bool $failure
* @return QueryBuilder
*/
public function getList(bool $failure = null): QueryBuilder
{
$subRepo = $this->getEntityManager()->getRepository(Service::class);
$query = $this->createQueryBuilder('n');
$subService = $subRepo->createQueryBuilder('ns');
return $query;
}
}
i chciałbym pobrać wpisy z Notification, które nie mają serwisów failure na true, czyli w mysql było by mniej więcej coś takiego
SELECT
n.*
FROM
`notifications` n
WHERE
(SELECT
s.id
FROM
`notification_services` ns
INNER JOIN
`services` s ON ns.service_id = s.id
WHERE
ns.notification_id = n.id
AND s.failure = 1
LIMIT 1) IS NULL;
lub też odpowiednio na odwrót - wpisy z Notification, które mają serwisy z failure na false. Jak to zrobić za pomocą query buildera, ewentualnie bezpośrednio w DQL?
Ten post edytował sebul 21.11.2017, 19:35:47