我即将输出一个包含多个文档的列表(称为弃权).但是并不是每个用户都应该被允许查看所有文档,因此我已经实现了一个过滤器来检查用户是否分配了相同的“航空公司”和“市场”.因此,每个用户只应该看到分配给他的“航空公司”和“市场”的文件.
这是f.e.用户实体的航空公司的吸气剂:
/**
* Get airlines
*
* @return array
*/
public function getAirlines()
{
if($this->airlines != null)
{
$airlines = explode(",", $this->airlines);
return $airlines;
}
return Array();
}
这是控制器逻辑:
//Get User:
$user = $this->container->get('security.context')->getToken()->getUser();
// Gets an Array of User markets
$user_markets = $user->getMarkets();
// Gets an Array of User carriers
$user_airlines = $user->getAirlines();
if(!$this->ROLE_IS(Array( 'ROLE_XY'))){
$query = $em->createquery(
'SELECT w
FROM WaiverBundle:Waiver w
WHERE w.carrier = :carrier
AND w.market = :market
ORDER BY w.id DESC'
)
->setFirstResult($page*10-10)
->setMaxResults(10)
// I wan't to get the whole array and not just one position here:
->setParameters(array(':carrier'=>$user_airlines[0],
':market'=>$user_markets[0],
));
}else{
$query = $em->createquery(
'SELECT u
FROM WaiverBundle:Waiver u
ORDER BY u.id DESC'
)
->setFirstResult($page*10-10)
->setMaxResults(10);
}
问题:如何将DQL属性与数组进行比较而不仅仅是将字符串作为参数进行比较?
解决方法:
我想你想使用“IN”语法,而不是“=”语法:
'SELECT w
FROM WaiverBundle:Waiver w
WHERE w.carrier IN (:carrier)
AND w.market = :market
ORDER BY w.id DESC'