sql - AND conditions in Doctrine -
i'm working doctrine 2 , came situation in want queries among few type of users.
type status active usera 0 0 userb 0 1 userc 1 1 userd 1 0
all queries wanna involves user a, user c , user d. 1 illustration next query:
public function countmonth(){ $date = new \datetime('now'); $newdate = $date->format('y-m'); $this->qb = $this->em->createquerybuilder(); $this->qb->select('count(u) cant') ->from('models\user', 'u') ->where('u.creation_date ?1') ->andwhere('u.status <> 0 , u.active <> 1'); //users b $this->qb->setparameter(1,"$newdate%"); $query = $this->qb->getquery(); homecoming $query->getsinglescalarresult(); }
i want know how query throws me users user b. because 1 wrote gives me usersd.
i know can in order users usersb, kind of annoys me write 2 lines of code inquire this. there more simplified way?
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) cant') ->from('models\user', 'u'); $andx->add('u.creation_date ?1'); $orx->add('u.status = 1 , u.active = 1 or u.active = 0'); $orx->add('u.status = 0 , u.active = 0'); $andx->add($orx); $this->qb->add('where', $andx); $this->qb->setparameter(1,"$newdate%"); $query = $this->qb->getquery(); homecoming $query->getsinglescalarresult(); }
you should utilize or
'u.status <> 0 or u.active <> 1'
the logic output 'u.status <> 0 , u.active <> 1'
true userd only beacuse both arguments must true when using and logical operator. for usera u.status <> 0 fails condition. for userc u.active <> 1 fails condition. now, using or following:
for usera:u.status <> 0
true , u.active <> 1
false, one true, pass for userb: u.status <> 0
false , u.active <> 1
false, both false, fails for userc: u.status <> 0
true , u.active <> 1
true, both true, pass for userd: u.status <> 0
false , u.active <> 1
true, one true, pass this way, rows userb passes condition.
--------- edit -----------
check out http://sqlfiddle.com/#!2/d3174/1/0
sql doctrine2 doctrine condition
No comments:
Post a Comment