Doctrine DQL中的嵌套查询
我首先想知道DQL中是否允许嵌套查询。我发现至少有一部分是(见)。所以我想知道他们是否能满足我的需要 情况如下:我有一个表Doctrine DQL中的嵌套查询,doctrine,dql,nested-queries,nested-query,Doctrine,Dql,Nested Queries,Nested Query,我首先想知道DQL中是否允许嵌套查询。我发现至少有一部分是(见)。所以我想知道他们是否能满足我的需要 情况如下:我有一个表ProjectNotification,其中有所有项目和所有用户的所有通知。然后,我有一个UserXNotification表,其中存储给定用户已读取的通知的ID。我希望能够提取每个项目的未读通知(不是由当前用户发起的)总数。以下查询有效(无内部选择)。但这一条给出了错误 $query = $em->createQuery(
ProjectNotification
,其中有所有项目和所有用户的所有通知。然后,我有一个UserXNotification
表,其中存储给定用户已读取的通知的ID。我希望能够提取每个项目的未读通知(不是由当前用户发起的)总数。以下查询有效(无内部选择)。但这一条给出了错误
$query = $em->createQuery(
'SELECT IDENTITY (n.project), COUNT(n.id)
FROM AppBundle:ProjectNotification n
WHERE n.project IN (:projectIDs)
AND n.user <> :user
AND n.id NOT IN (
SELECT uxn.projectNotification
FROM AppBundle:UserXNotification uxn
WHERE uxn.user = :user
AND uxn.read = false
)
GROUP BY n.project
')->setParameter('projectIDs', $projectIDs)
->setParameter('user', $user);
$notifQueryResult = $query->getResult();
为了完整起见,这里是UsersXNotifications实体:
class UserXNotification
{
/**
* @ORM\Column(type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\ManyToOne(targetEntity="User")
*/
protected $user;
/**
* @ORM\ManyToOne(targetEntity="ProjectNotification")
*/
protected $projectNotification;
/**
* @ORM\Column(type="boolean")
*/
protected $readStatus;
这是ProjectNotification实体
class ProjectNotification
{
/**
* @ORM\Column(type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
// SOME IRRELEVANT FIELDS
/**
* @ORM\ManyToOne(targetEntity="Project")
*/
protected $project;
/**
* @ORM\ManyToOne(targetEntity="User")
*/
protected $user;
实际上,我编写的查询的第一个版本是选择uxn.projectNotification.id
而不是选择uxn.projectNotification
,但它给了我以下错误:
QueryException: [Semantical Error] line 0, col 378 near 'id': Error: Class AppBundle\Entity\UserXNotification has no field or association named projectNotification.id
我还尝试:
...
AND n NOT IN (
SELECT uxn.projectNotification
无济于事。
我知道内部查询正在检索对象,这就是我尝试最后一个查询的原因。本例中的错误是:
QueryException: [Semantical Error] line 0, col 355 near 'projectNotification': Error: Invalid PathExpression. Must be a StateFieldPathExpression.
我希望这是可行的,而不必写在下面:/
有什么提示吗?我解决了,我必须使用IDENTITY()。我解决了,我必须使用IDENTITY()。
QueryException: [Semantical Error] line 0, col 355 near 'projectNotification': Error: Invalid PathExpression. Must be a StateFieldPathExpression.