Laravel Query Builder :: whereNotNull导致重复WHERE NOT NULL语句?

时间:2015-06-12 23:28:14

标签: php sql-server laravel laravel-5 query-builder

我得到了奇怪的行为,并且不确定这是我做错了什么还是Laravel查询构建器中的错误。

每当我使用Builder::whereNotNull方法时,我的查询中似乎总是至少有一个WHERE NOT NULL语句。

示例代码:

同步所有客户联系人的方法:

public function doStuff(CustomerRepository $customers)
{
    $count = $customers->count();

    $pages = (integer) ceil($count / $this->pageSize);

    for ($page = 0; $page < $pages; $page++) {
        $contacts = $customers->page($page * $this->pageSize, $this->pageSize);
        // yada-yada
    }
}

Customer Repository类中的相关方法:

public function __construct(DatabaseManager $databaseManager)
{
    $this->customers = $databaseManager->connection('sqlsrv')
        ->table('dbo.entity as Contact')
        ->select($this->select)
        ->leftJoin('dbo.entity AS Parent', 'Contact.COMPANY_ID', '=', 'Parent.ENTITY_ID')
        ->whereNotNull('Contact.email');
}

public function page($offset = 0, $count = 1000)
{
    if ($count > 1000) {
        $count = 1000;
    }

    return $this->customers
        ->skip($offset)
        ->take($count)
        ->get();
}

最终会产生如下查询:

    select * from (
        select [Contact].[email],
            [Parent].*,
            row_number() over (order by (select 0)) as row_num
        from [dbo].[entity] as [Contact]
        left join [dbo].[entity] as [Parent] on [Contact].[COMPANY_ID] = [Parent].[ENTITY_ID]
        where [Contact].[email] is not null and [Contact].[email] is not null and [Contact].[email] is not null and [Contact].[email] is not null and [Contact].[email] is not null and [Contact].[email] is not null and [Contact].[email] is not null and [Contact].[email] is not null and [Contact].[email] is not null and [Contact].[email] is not null and [Contact].[email] is not null and [Contact].[email] is not null and [Contact].[email] is not null and [Contact].[email] is not null and [Contact].[email] is not null and [Contact].[email] is not null and [Contact].[email] is not null and [Contact].[email] is not null and [Contact].[email] is not null and [Contact].[email] is not null and [Contact].[email] is not null and [Contact].[email] is not null and [Contact].[email] is not null and [Contact].[email] is not null and [Contact].[email] is not null and [Contact].[email] is not null and [Contact].[email] is not null and [Contact].[email] is not null and [Contact].[email] is not null and [Contact].[email] is not null and [Contact].[email] is not null and [Contact].[email] is not null and [Contact].[email] is not null and [Contact].[email] is not null and [Contact].[email] is not null and [Contact].[email] is not null and [Contact].[email] is not null and [Contact].[email] is not null and [Contact].[email] is not null and [Contact].[email] is not null and [Contact].[email] is not null and [Contact].[email] is not null and [Contact].[email] is not null and [Contact].[email] is not null and [Contact].[email] is not null and [Contact].[email] is not null and [Contact].[email] is not null and [Contact].[email] is not null and [Contact].[email] is not null and [Contact].[email] is not null and [Contact].[email] is not null and [Contact].[email] is not null and [Contact].[email] is not null and [Contact].[email] is not null and [Contact].[email] is not null and [Contact].[email] is not null and [Contact].[email] is not null and [Contact].[email] is not null and [Contact].[email] is not null and [Contact].[email] is not null and [Contact].[email] is not null and [Contact].[email] is not null and [Contact].[email] is not null and [Contact].[email] is not null and [Contact].[email] is not null and [Contact].[email] is not null
        ) as temp_table
    where row_num between 64001 and 65000

1 个答案:

答案 0 :(得分:0)

我必须与循环相关。因为我看到每次循环时sql查询都会附加一个&#34;其中not null&#34; (65次循环,65次出现&#34;其中不为空&#34;

你是否尝试使用与whereNotNull不同的方法,我认为问题不在于whereNotNull方法。它必须与循环有关。

我需要查看整个类以及从该类创建对象的方式来解决问题。