0

I have created a select query that functioning well. But now I need to change this to a like query. Can anyone tell me the correct way?

This is my select query structure ->

public function getSelectedHotel($id)
{
    $selectedHotel = $this->tableGateway->select(['hotel_id' => $id]);

        return $selectedHotel;
}

When I code like this it gives me this error.

Code ->

$hotelsByYear = $this->tableGateway->select()->where('date_added like ?',$year.'%');

Error ->

Fatal error: Uncaught Error: Call to undefined method Zend\Db\ResultSet\ResultSet::where() in

i am batman
  • 581
  • 3
  • 6
  • 21
  • Possible duplicate of [Zend 2 - TableGateway Where Clauses](https://stackoverflow.com/questions/13334220/zend-2-tablegateway-where-clauses) – Dymen1 May 27 '17 at 20:18

4 Answers4

1

Please try below code OR refer the official document.

$where = new Where();    
$where->like('Field_Name', '%'.$ParamVal.'%');
//use as below
$this->tableGateway->select($where);

Don't forget to use Zend\Db\Sql\Where;

Hope this helps

Patrick R
  • 6,621
  • 1
  • 24
  • 27
0

Please have a look at the documentation: https://framework.zend.com/manual/2.2/en/modules/zend.db.table-gateway.html#basic-usage

It should be something like:

$hotelsByYear = $this->tableGateway->select(function (Select $select) {
    $select->where->like('date_added', $year.'%');
});

Explanation: Everytime you call $tableGateway->select() the query will be executed, so if you do $tableGateway->select()->where() you call ->where() on the result of the ->select() method, which is obviously a Zend\Db\ResultSet\ResultSet.

NiMeDia
  • 995
  • 1
  • 15
  • 27
0

$table->select()->where('Field_Name', '%'.$ParamVal.'%');

Gaurav Sharma
  • 331
  • 2
  • 10
0

Another option is to use a Predicate :

$selectedHotel = $this->tableGateway->select([
    'hotel_id' => $id,
    new \Zend\Db\Sql\Predicate\Like('date_added', $year.'%')
]);