0
votes

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

4

4 Answers

1
votes

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

0
votes

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.

0
votes

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

0
votes

Another option is to use a Predicate :

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