簡體   English   中英

symfony學說復雜的查詢構建器

[英]symfony doctrine complex querybuilder

也許很簡單,但是我的查詢生成器沒有返回我想要的東西。 我會喜歡...在哪里... AND(.... OR ....)

我嘗試閱讀該學說文檔,但是我英語不流利,所以我不太了解該學說文檔(也許可以用法語翻譯?)。

<?php
// $qb instanceof QueryBuilder

$qb->select(array('u')) // string 'u' is converted to array internally
   ->from('User', 'u')
   ->where($qb->expr()->orX(
       $qb->expr()->eq('u.id', '?1'),
       $qb->expr()->like('u.nickname', '?2')
   ))
   ->orderBy('u.surname', 'ASC'));

這是我的MySQL測試代碼

select count(distinct(u.username)) from fos_user u join users_accounts ua ON u.id=ua.user_id join account acc ON acc.id = ua.account_id where u.id=48 and (acc.id=2 OR acc.id=5)

這是查詢生成器:

我使用服務是因為我不得不多次使用此功能。

/**
* Check if User exists in one of the connected user (Admin or SupAdmin) accounts
* argument : userID
* 
*/
public function userExists($userID)
    {       
        // Return accounts (array)
        $accounts   = $this->accountManager->listAccountsByConnectedUser();
        $repository = $this->em->getRepository('CMiNewsBundle:User');

        $qb = $repository->createQueryBuilder('u');

        $query = $qb
                    ->select('count(u)')
                    ->join ('u.accounts', 'acc')
                    ->where('u.id = :userID')
                    ->andwhere('acc.id = :accountID')
                    ->setParameters(array(
                      'userID'  => $userID,
                      'accountID'  => $accounts[0]->getId(),
                    ));

                   if (count($accounts) > 1) {


                    $accountMax = count($accounts);                         

                    for($acc=1; $acc<$accountMax; $acc++)
                    {
                        $query->orWhere('acc.id = :accountID_'.$acc.'')->setParameter('accountID_'.$acc.'', $accounts[$acc]->getId());
                    }
                   };

                   $query = $query->getQuery();
                   $result = $query->getResult();
        return $result;
    }

謝謝你的建議

您可以使用mysql中的IN()重寫查詢,如下所示

SELECT 
  COUNT(DISTINCT (u.username)) 
FROM
  fos_user u 
  JOIN users_accounts ua 
    ON u.id = ua.user_id 
  JOIN account acc 
    ON acc.id = ua.account_id 
WHERE u.id = 48 
  AND acc.id IN(2,5) /* this is equivalent to acc.id = 2 or acc.id = 5 */

您正在運行的當前學說查詢忽略了您和條件,例如如果您有2個帳戶ID,則查詢為

WHERE u.id = 48 AND acc.id = 2 OR acc.id = 5

因此它將給出acc.id = 5記錄,而u.id可以是任何其他id


對於學說查詢,您可以按以下方式重寫代碼,您必須構建一個帳戶ID數組,並將此數組傳遞到IN()子句中

public function userExists($userID)
{
    // Return accounts (array)
    $accounts = $this->accountManager->listAccountsByConnectedUser();
    $repository = $this->em->getRepository('CMiNewsBundle:User');
    $qb = $repository->createQueryBuilder('u');
    $accounts=array();
    $accounts[]=$accounts[0]->getId();
    if (count($accounts) > 1) {
        $accountMax = count($accounts);
        for ($acc = 1; $acc < $accountMax; $acc++) {
            $accounts[]=$accounts[$acc]->getId();
        }
    }
    $query = $qb
        ->select('count(u) as your_count')
        ->join('u.accounts', 'acc')
        ->where('u.id = :userID')
        ->andwhere('acc.id IN(:accountID)')
        ->setParameters(array(
            'userID' => $userID,
            'accountID' => $accounts,
        ));
    $query = $query->getQuery();
    $result = $query->getResult();
    return $result;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM