将 pgSQL 转换为 Doctrine

Converting pgSQL to Doctrine

我收到错误 [语法错误] 第 0 行,第 257 列:错误:应为 =、<、<=、<>、>、>=、!=、得到“18” 当这个 Doctrine Query Builder 查询执行时。数据库是pgSQL。

        $query = $this->entityManager
            ->createQueryBuilder()
            ->select('s.subscriptionReference')
            ->from(
                'OTDatabase\Entity\Membership\Member',
                'm'
            )
            ->innerJoin(
                'OTDatabase\Entity\Membership\MemberSubscriptions',
                's',
                'WITH',
                'm.record = s.memberReference'
            );

        $query
            ->andWhere('s.list = 1');

        $query
            ->andWhere('m.validated IN (:validated)')
            ->setParameter('validated', array('1', '3'));

        $query
            ->andWhere($query->expr()->lt(gmdate('Y-m-d H:i:s') , 's.subscription_begins'));

        $query
            ->andWhere('
            (
                ( now() <= s.subscriptionExpires )
                OR
                ( s.subscriptionBegins <= now() AND s.subscriptionExpires IS NULL )
            )');

        $result = $query->getQuery()
                        ->getArrayResult();

为了将来参考,正确的查询(在@Logan Bailey 的帮助下)是:

        $query = $this->entityManager
            ->createQueryBuilder()
            ->select('s.subscriptionReference')
            ->from(
                'OTDatabase\Entity\Membership\Member',
                'm'
            )
            ->innerJoin(
                'OTDatabase\Entity\Membership\MemberSubscriptions',
                's',
                'WITH',
                'm.record = s.memberReference'
            );

        $query
            ->andWhere('s.list = 1');

        $query
            ->andWhere('m.validated IN (:validated)')
            ->setParameter('validated', array('1', '3'));

        $query
            ->andWhere($query->expr()->lt('s.subscriptionBegins' , ':subscriptionDate'))
            ->setParameter('subscriptionDate', gmdate('Y-m-d H:i:s'));

        $query
            ->andWhere($query->expr()->orX(
                $query->expr()->lt(':subscriptionDate' , 's.subscriptionExpires'),
                $query->expr()->isNull('s.subscriptionExpires')
            ));

您必须将日期用引号引起来。您可能应该将其用作绑定参数。

$query
   ->andWhere($query->expr()->lt(':subscriptionDate' , 's.subscription_begins'))
   ->setParameter('subscriptionDate', gmdate('Y-m-d H:i:s'));