$condition 里面会有时间的条件,这个时间的条件是可选条件,现在就想如果时间条件不存在如何把时间条件给去除?而时间条件存在时,则加上时间条件查询结果?
方法一:数组不存在会报错。
public static function getHomeworkByStudentId($id, $conditions)
{
$conditions['startTime'] = '2018-09-01';
$conditions['endTime'] = '2018-09-02';
$result = self::where('id', '=', $id)
->whereTime('recordTime', '>', $conditions['startTime'])
->whereTime('recordTime', '
->select();
return $result;
}
方法二:如果时间条件不存在,生成sql的时间条件就会变成0就像下面这样:
SELECT * FROM `homework` WHERE `id` = '1' AND ( `recordTime` >= 0 AND `recordTime` <= 0 )
public static function getHomeworkByStudentId($id, $conditions)
{
$conditions['startTime'] = '2018-09-01';
$conditions['endTime'] = '2018-09-02';
$result = self::where('id', ':id')
->whereTime('recordTime', '>=', ':startTime')
->whereTime('recordTime', '<=', ':endTime')
->bind($conditions)
->select();
return $result;
}