0

在哪里进行这样的查询,我该怎么办?

SELECT * FROM hotels WHERE name LIKE '%mykey%' AND country_id = '12'

我已经这样做了,但我们不能:

$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();

有义务使 if 语句改变查询的行为。我能怎么做?

4

1 回答 1

1

$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();
于 2013-09-27T08:25:59.463 回答