Where to make a query like this, how can I do?
SELECT * FROM hotels WHERE name LIKE '%mykey%' AND country_id = '12'
I have done so, but we must not:
$select = $this->sql->select();
$select->from(self::TABLE);
if ($params['key'])
{
$where['where'] = new \Zend\Db\Sql\Predicate\Like('name', '%'.$params['key'].'%');
}
if($params['country_id'])
{
$where['where'] = array(
'country_id' => $params['country_id']
);
}
$select->where($where['where']);
$select->order('id DESC');
$statement = $this->sql->prepareStatementForSqlObject($select);
return $statement->execute();
are obliged to make the if statement to change the behavior of the query. How can I do?
$select = $this->sql->select();
$select->from(self::TABLE);
if ($params['key'])
{
$select->where->like('name', '%'.$params['key'].'%');
}
if($params['country_id'])
{
$select->where(array('country_id' => $params['country_id']));
}
$select->order('id DESC');
$statement = $this->sql->prepareStatementForSqlObject($select);
return $statement->execute();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.