select - ZF2 Greater than / Less than query -


sql query

select * table_name  category = 'category_name'  , created < 'todays_date'  , created > 'yesterdays_date' 

also need add order , limit conditions. how can achieve in zf2?

i have code:

$rowset = $this->tablegateway->select(function ($select) {         $select->where(['category' => $this->category]);         $select->order('id desc');         $select->limit($this->limit);          $dbtimenow = new \datetime();         $select->lessthanorequalto("created", $dbtimenow->format('y-m-d h:i:s'));         $dbtimenow->sub(new dateinterval('p1d'));         $select->greaterthanorequalto("created", $dbtimenow->format('y-m-d h:i:s'));     }); 

you need use predicate object want use operators: <= , >= in case. can using component zend\db\sql\where of zend\db of zf2 extends zend\db\sql\predicate. , able use operators when need. please check out following:

$select = $this->tablegateway->getsql()->select();  // here catch $predicate = new  \zend\db\sql\where();  $select->where(['category' => $this->category]);  // use $dbtimenow = new \datetime(); $select->where($predicate->lessthanorequalto("created", $dbtimenow->format('y-m-d h:i:s')), 'and'); $dbtimenow->sub(new dateinterval('p1d')); $select->where($predicate->greaterthanorequalto("created", $dbtimenow->format('y-m-d h:i:s')), 'and');  $select->order('id desc'); $select->limit($this->limit);  $resultset = $this->tablegateway->selectwith($select); 

Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -