【问题标题】:How to convert this query to doctrine DQL如何将此查询转换为学说 DQL
【发布时间】:2017-03-28 19:36:10
【问题描述】:
SELECT apntoken,deviceid,created 
FROM `distribution_mobiletokens` as dm
WHERE userid='20'
and not exists (
    select 1 
    from `distribution_mobiletokens`
    where userid = '20'
    and deviceid = dm.deviceid
    and created > dm.created
    )

此查询的作用是选择用户 ID 等于 20 且设备 ID 相同但为设备选择最新的 apntoken 的所有移动令牌。

我的数据库如下所示。

有关此查询的更多信息,我从我在这里提出的另一个问题中得到了这个答案(How to group by in SQL by largest date (Order By a Group By))

我尝试过的事情

$mobiletokens = $em->createQueryBuilder()
          ->select('u.id,company.id as companyid,user.id as userid,u.apntoken')
          ->from('AppBundle:MobileTokens', 'u')
          ->leftJoin('u.companyId', 'company')
          ->leftJoin('u.userId', 'user')
          ->where('u.status = 1 and user.id = :userid')
          ->setParameter('userid',(int)$jsondata['userid'])
          ->groupby('u.apntoken')
          ->getQuery()
          ->getResult();

       //@JA - Get the list of all the apn tokens we need to send the message to.
       foreach($mobiletokens as $tokenobject){
           $deviceTokens[] = $tokenobject["apntoken"];
           echo $tokenobject["apntoken"]."\n";
       }

        die();

这给了我不正确的反应

63416A61F2FD47CC7B579CAEACB002CB00FACC3786A8991F329BB41B1208C4BA
9B25BBCC3F3D2232934D86A7BC72967A5546B250281FB750FFE645C8EB105AF6
latestone

感谢您的帮助!

其他信息

SELECT * FROM 的数据

使用我提供的 SQL 后的数据。

【问题讨论】:

  • 如果我错过了任何有用的信息,请告诉我,我还在做更多的研究,在这种情况下不使用 DQL 并使用直接 SQL 的最佳方法是什么?

标签: mysql symfony doctrine-orm dql doctrine-query


【解决方案1】:

您可以使用使用查询构建器创建的子选择作为示例:

public function selectNewAppToken($userId)
{
    // get an ExpressionBuilder instance, so that you
    $expr = $this->_em->getExpressionBuilder();

    // create a subquery in order to take all address records for a specified user id
    $sub = $this->_em->createQueryBuilder()
        ->select('a')
        ->from('AppBundle:MobileTokens', 'a')
        ->where('a.user = dm.id')
        ->andWhere('a.deviceid = dm.deviceid')
        ->andWhere($expr->gte('a.created','dm.created'));


    $qb = $this->_em->createQueryBuilder()
        ->select('dm')
        ->from('AppBundle:MobileTokens', 'dm')
        ->where($expr->not($expr->exists($sub->getDQL())))
        ->andWhere('dm.user = :user_id')
        ->setParameter('user_id', $userId);

    return $qb->getQuery()->getResult();
}

【讨论】:

  • 顺便谢谢你,我稍后再试试!
【解决方案2】:

我暂时这样做是为了临时修复,但不确定这是否是最佳答案。

$em = $this->em;

       $connection = $em->getConnection();
       $statement = $connection->prepare("
            SELECT apntoken,deviceid,created 
            FROM `distribution_mobiletokens` as dm
            WHERE userid=:userid
            and not exists (
                select 1 
                from `distribution_mobiletokens`
                where userid = :userid
                and deviceid = dm.deviceid
                and created > dm.created
            )");
       $statement->bindValue('userid', $jsondata['userid']);
       $statement->execute();

       $mobiletokens = $statement->fetchAll();

       //@JA - Get the list of all the apn tokens we need to send the message to.
       foreach($mobiletokens as $tokenobject){
           $deviceTokens[] = $tokenobject["apntoken"];
           echo $tokenobject["apntoken"]."\n";
       }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-11
    • 1970-01-01
    • 1970-01-01
    • 2012-10-21
    • 2015-04-02
    • 2016-02-29
    相关资源
    最近更新 更多