您好我有DQL查询:
'SELECT t FROM VputiTripBundle:Trip t WHERE IDENTITY(t.profile) != :profile AND (t.startCity = :param0 OR t.targetCity = :param0) AND (t.startCity = :param1 OR t.targetCity = :param1) AND ...
我ant这样做(就像这样)
'SELECT t FROM VputiTripBundle:Trip t WHERE IDENTITY(t.profile) != :profile AND (t.startCity = :param0 OR t.targetCity = :param0) OR (t.startCity = :param1 OR t.targetCity = :param1) OR ...
这是我的查询构建器函数:
public function selectRelatedTrips($assoc, $profileId)
{
$query = $this
->getEntityManager()
->createQueryBuilder()
->select('t')
->from('VputiTripBundle:Trip', 't')
->where('IDENTITY(t.profile) != :profile');
$params['profile']=$profileId;
foreach ($assoc as $k => $v) {
$query->andWhere('t.startCity = :param' . $k . ' OR t.targetCity = :param' . $k);
$params['param' . $k] = $v;
}
return $query->setParameters($params)
->setMaxResults(20)
->orderBy('t.id', 'desc')
->getQuery()
->getResult();
}
我怎么能这样做?
答案 0 :(得分:1)
尝试此查询:
$cities = array();
foreach ($assoc as $k => $v) {
$cities[] = $v;
}
$params = array(
'profile' => $profileId,
'cities' => $cities
);
$query = $this->getEntityManager()
->createQueryBuilder()
->select('t')
->from('VputiTripBundle:Trip', 't');
$query->where(
$query->expr()->andX(
$query->expr()->neq('IDENTITY(t.profile)', ':profile'),
$query->expr()->orX(
$query->expr()->in('t.startCity', ':cities'),
$query->expr()->in('t.targetCity', ':cities')
)
)
);
return $query->setParameters($params)
->setMaxResults(20)
->orderBy('t.id', 'desc')
->getQuery()
->getResult();