Php 如何制作模型->;yii2中的搜索函数不返回空值行

Php 如何制作模型->;yii2中的搜索函数不返回空值行,php,activerecord,yii2,Php,Activerecord,Yii2,因此,我有一个具有关系的模型,我修改了搜索函数以允许查询关系数据,但我不希望它返回其中包含空值的条目。以下是我的搜索功能: public function search($params) { $query = Services::find(); $query->joinWith(['location', 'client', 'operator']); $dataProvider = new ActiveDataProvider([ 'query' =

因此,我有一个具有关系的模型,我修改了搜索函数以允许查询关系数据,但我不希望它返回其中包含空值的条目。以下是我的搜索功能:

public function search($params)
{
    $query = Services::find();
    $query->joinWith(['location', 'client', 'operator']);
    $dataProvider = new ActiveDataProvider([
        'query' => $query,
        'sort'=> ['defaultOrder' => ['date'=>SORT_ASC]]
    ]);

...
    $query->andFilterWhere([
        'id' => $this->id,
        'tip' => $this->tip,
        'status' => $this->status,
    ]);
    if ( ! is_null($this->date) && strpos($this->date, ' - ') !== false ) {
    $datelist = explode(' - ', $this->date);
    // var_dump($datelist);
    // die($datelist);
    $start_date = \DateTime::createFromFormat('m-d-Y H:i:s', $datelist[0].' 00:00:00');
    $end_date = \DateTime::createFromFormat('m-d-Y H:i:s', $datelist[1].' 23:59:59');
    $query->andFilterWhere(['between', 'date', $start_date->format('Y-m-d H:i:s'),$end_date->format('Y-m-d H:i:s')]);
    }
    $query->andFilterWhere(['like', 'location.address', $this->location]);
    $query->andFilterWhere(['like', 'client.name', $this->client]);
    $query->andFilterWhere(['like', 'personal.nume', $this->operator_id]);
    $query->andFilterWhere(['NOT', [$this->client=>null]]);
    $query->andFilterWhere(['NOT', [$this->location=>null]]);
    $query->andFilterWhere(['NOT', ['location_id' => null]]);
    $query->andFilterWhere(['NOT', ['client_id' => null]]);

    return $dataProvider;
}
请注意最后4个查询行。我认为应该忽略那些特定属性中具有空值的行

这是生成的查询,它不包含任何not条件,我不知道为什么

SELECT `services`.* FROM `services` LEFT JOIN `location` ON `services`.`location_id` = `location`.`id` LEFT JOIN `client` ON `services`.`client_id` = `client`.`id` LEFT JOIN `personal` ON `services`.`operator_id` = `personal`.`id` WHERE `date` BETWEEN '2015-01-01 00:00:00' AND '2015-01-22 23:59:59' ORDER BY `client`.`name` DESC LIMIT 20
如果以前有人这样做过,请演示如何:D


提前感谢您

而不是使用ANDFILTER此处使用andWhere。andFilterWhere忽略空操作数,因此不会将空值添加到查询中

替换

$query->andFilterWhere(['NOT', [$this->client=>null]]);
$query->andFilterWhere(['NOT', [$this->location=>null]]);
$query->andFilterWhere(['NOT', ['location_id' => null]]);
$query->andFilterWhere(['NOT', ['client_id' => null]]);


使用andWhere代替ANDFILTER。andFilterWhere忽略空操作数,因此不会将空值添加到查询中

替换

$query->andFilterWhere(['NOT', [$this->client=>null]]);
$query->andFilterWhere(['NOT', [$this->location=>null]]);
$query->andFilterWhere(['NOT', ['location_id' => null]]);
$query->andFilterWhere(['NOT', ['client_id' => null]]);