0
我正在與Doctrine 2,我想出了這種情況,我只想在幾種類型的用戶之間做一些查詢。AND條件在學說
Type Status Active
UserA 0 0
UserB 0 1
UserC 1 1
UserD 1 0
所有我想要做的查詢涉及用戶A,用戶C和用戶D的一個例子是這樣的一個查詢:
public function countMonth(){
$date = new \Datetime('now');
$newDate = $date->format('Y-m');
$this->qb = $this->em->createQueryBuilder();
$this->qb->select('count(u) as cant')
->from('models\User', 'u')
->where('u.creation_date like ?1')
->andWhere('u.status <> 0 AND u.active <> 1'); //users B
$this->qb->setParameter(1,"$newDate%");
$query = $this->qb->getQuery();
return $query->getSingleScalarResult();
}
我想知道如何做到這一點拋出我回來查詢所有的用戶,但用戶B.因爲我寫的只是給我的用戶D。
我知道我可以做到這一點,以獲得所有用戶,但用戶B,但它有點讓我很煩惱寫兩行代碼要求。有更簡化的方法嗎?
public function countMonth(){
$orX = $this->qb->expr()->orX();
$andX = $this->qb->expr()->andX();
$date = new \Datetime('now');
$newDate = $date->format('Y-m');
$this->qb = $this->em->createQueryBuilder();
$this->qb->select('count(u) as cant')
->from('models\User', 'u');
$andX->add('u.creation_date like ?1');
$orX->add('u.status = 1 AND u.active = 1 OR u.active = 0');
$orX->add('u.status = 0 AND u.active = 0');
$andX->add($orX);
$this->qb->add('where', $andX);
$this->qb->setParameter(1,"$newDate%");
$query = $this->qb->getQuery();
return $query->getSingleScalarResult();
}
因此,任何的成功嗎? – fkupper 2014-11-04 17:23:04