0
這裏是我的DB模式:如何在DQL條件下使用MAX函數?
| User | | View |
*-------* *----------*
| id | | id |
| date |
| Movie | | movie_id |
*-------* | user_id |
| id |
| title | | Comment |
| slug | *-----------*
| cover | | id |
| createdAt |
| view_id |
| user_id |
我試圖做到的,是選擇具有至少一個視圖不是在電影發佈針對特定用戶的任何評論較早的電影,或者從未被評論由這個用戶。
我使用學說2,這裏就是我迄今所做的:
$this->createQueryBuilder('movie')
->select('DISTINCT movie.id', 'movie.title', 'movie.slug', 'movie.cover')
->addSelect('MAX(view.date) as lastViewedOn')
->addSelect('MAX(comment.createdAt) as lastCommentedOn')
->innerJoin('movie.views', 'view', 'WITH', 'view.user = :user')
->leftJoin('movie.comments', 'comment', 'WITH', 'comment.author = :user')
->andWhere(
$qb->expr()->orX('lastCommentedOn IS NULL', 'lastViewedOn > lastCommentedOn')
)
->orderBy('lastViewedOn', 'DESC')
->setParameter('user', $user)
->getQuery()
->getScalarResult()
;
問題是,這個請求會拋出異常:
QueryException: [Semantical Error] line 0, col 410 near 'lastCommentedOn': Error: 'lastCommentedOn' does not point to a Class.
,我真的不明白爲什麼...
感謝您的啓示。