最初に、ネストされたクエリが DQL で許可されているかどうか疑問に思いました。少なくとも部分的にはあることがわかりました (こちらを参照)。だから私は彼らが私が必要とすることをすることができるかどうか疑問に思っています.
状況は次のとおり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();
具体的なエラーは次のとおりです。
QueryException: [Semantical Error] line 0, col 354 near 'projectNotification': Error: Invalid PathExpression. Must be a StateFieldPathExpression.
完全を期すために、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;
実際、私が書いたクエリの最初のバージョンはSELECT uxn.projectNotification.id
代わりにありSELECT 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.
ネイティブSQLと水和物を書かなくても実行できることを願っています:/
ヒントはありますか?