/**
* 判断是否添加where语句
* @param $sql
* @param $has_where
* @return string
*/
public function append_where(&$sql, $has_where) {
$sql .= $has_where ? ' AND ' : ' WHERE ';
return $sql;
}
/**
* 检索数据
* @param $params
* @return ActiveDataProvider
*/
public function search($params)
{
$connection = Yii::$app->db;
$sql = "SELECT * FROM specification ";
$has_where = FALSE;
//是否选择了应用
if(!empty($params['application_id']))
{
$has_where = $this->append_where($sql, $has_where);
$sql .= " (application_category LIKE '{$params['application_id']},%' or application_category LIKE '%,
{$params['application_id']}' or application_category LIKE '%,
{$params['application_id']}%' or application_category={$params['application_id']})";
}
//是否填写了分子下限
if(!empty($params['weight_low']))
{
$has_where = $this->append_where($sql, $has_where);
$sql .= " molecular_weight_lower>={$params['weight_low']}";
}
//是否填写了分子上限
if(!empty($params['weight_up']))
{
$has_where = $this->append_where($sql, $has_where);
$sql .= " molecular_weight_upper<={$params['weight_up']}";
}
//是否填写了醇解度下限
if(!empty($params['alcoholysis_low']))
{
$has_where = $this->append_where($sql, $has_where);
$sql .= " alcoholysis_lower>={$params['alcoholysis_low']}";
}
//是否填写了醇解度上限
if(!empty($params['alcoholysis_up']))
{
$has_where = $this->append_where($sql, $has_where);
$sql .= " alcoholysis_upper<={$params['alcoholysis_up']}";
}
$command = $connection->createCommand($sql);
$res = $command->queryAll();
return $res;
}