Php symfony条令复杂查询生成器
可能是一些简单的东西,但我的查询生成器没有返回我想要的。我想要点。。。哪里和(……或……) 我试着阅读《条令》文件,但我英语不流利,所以我不太理解《条令》文件(可能有法语翻译?) 这是查询生成器: 我使用服务是因为我必须多次使用此功能Php symfony条令复杂查询生成器,php,symfony,doctrine-orm,doctrine,query-builder,Php,Symfony,Doctrine Orm,Doctrine,Query Builder,可能是一些简单的东西,但我的查询生成器没有返回我想要的。我想要点。。。哪里和(……或……) 我试着阅读《条令》文件,但我英语不流利,所以我不太理解《条令》文件(可能有法语翻译?) 这是查询生成器: 我使用服务是因为我必须多次使用此功能 /** * Check if User exists in one of the connected user (Admin or SupAdmin) accounts * argument : userID * */ public function userE
/**
* 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;
}
/**
*检查连接的用户(管理员或管理员)帐户之一中是否存在用户
*参数:userID
*
*/
公用函数userExists($userID)
{
//返回帐户(数组)
$accounts=$this->accountManager->listAccountsByConnectedUser();
$repository=$this->em->getRepository('CMiNewsBundle:User');
$qb=$repository->createQueryBuilder('u');
$query=$qb
->选择(‘计数(u)’)
->加入(‘美国账户’、‘acc’)
->其中('u.id=:userID')
->andwhere('acc.id=:accountID')
->设置参数(数组)(
“userID”=>$userID,
“accountID'=>$accounts[0]->getId(),
));
如果(计数($accounts)>1){
$accountMax=计数($accounts);
对于($acc=1;$accorWhere('acc.id=:accountID.'$acc.')->设置参数('accountID.'$acc.',$accounts[$acc]->getId());
}
};
$query=$query->getQuery();
$result=$query->getResult();
返回$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 */
您正在运行的条令的当前查询将忽略您的条件,例如如果您有两个帐户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;
}
公共函数userExists($userID)
{
//返回帐户(数组)
$accounts=$this->accountManager->listAccountsByConnectedUser();
$repository=$this->em->getRepository('CMiNewsBundle:User');
$qb=$repository->createQueryBuilder('u');
$accounts=array();
$accounts[]=$accounts[0]->getId();
如果(计数($accounts)>1){
$accountMax=计数($accounts);
对于($acc=1;$acc<$accountMax;$acc++){
$accounts[]=$accounts[$acc]->getId();
}
}
$query=$qb
->选择('count(u)作为您的_count')
->加入(‘美国账户’、‘acc’)
->其中('u.id=:userID')
->andwhere('acc.id IN(:accountID'))
->设置参数(数组)(
“userID”=>$userID,
“accountID”=>$accounts,
));
$query=$query->getQuery();
$result=$query->getResult();
返回$result;
}
@MkJ:thx用于此。一个问题,此代码是否仅返回acc=2或acc=5或2,3,4,5?@BenjaminLucas此代码将适用于您的帐户ID,如果您有2,3,4,5,则它将给出2,3,4,5,如果您有2,5,则返回2,5
WHERE u.id = 48 AND acc.id = 2 OR acc.id = 5
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;
}