带有orderby子查询和分页的错误的查询绑定顺序

时间:2019-09-16 14:42:10

标签: php laravel laravel-6

我整天都在为这个问题而烦恼,这是我的情况。

我刚刚为较新的子查询升级到Laravel 6.x(直到昨天我对DB :: raw查询进行了所有操作,并且一切都按预期进行了。)

该数据库是一个SQL Server。

我有两个涉及范围的模型:

客户

class Cliente extends Model {

    protected static function boot() {
        parent::boot();

        static::addGlobalScope('cliente', function (Builder $builder) {
            $builder->where('ANTIPCON', 'C');
        });
    }

    public function documenti(){
        return $this->hasMany('App\DocumentoVendita', 'MVCODCON','ANCODICE');
    }

}

Documento

class DocumentoVendita extends Model {

    protected static function boot() {
        parent::boot();

        static::addGlobalScope('documento_vendita', function (Builder $builder) {
            $builder
                ->where('MVFLVEAC', 'V')
                ->where(function($query){
                    $query
                        ->where('MVCLADOC', 'FA')
                        ->orWhere('MVCLADOC', 'NC');
                });
        });
    }


    public function cliente() {
        return $this->hasOne('App\Cliente', 'ANCODICE', 'MVCODCON');
    }

}

在我的方法中,我用其他模型的SUM列出了Clienti模型的简单列表: 在这个特定的代码段中,为简单起见,我删除了SUM,我已经用SubQuery替换了它们,它们的工作原理就像是一种魅力。

$clienti = Cliente::select(
    'ANCODICE', 'ANDESCRI', 'ANLOCALI', 'ANCODFIS', 'ANPARIVA',
    'ANCODAG1', 'AGDESAGE'
    ,DB::raw('0 AS FatturatoPrecedente')
    ,DB::raw('0 AS Fatturato')
)
->groupBy('ANCODICE')
->groupBy('ANDESCRI')
->groupBy('ANLOCALI')
->groupBy('ANCODFIS')
->groupBy('ANPARIVA')
->whereRaw("format(ANDTOBSO, 'yyyy-MM-dd hh:mm:ss') >= '". \Carbon\Carbon::now()->format('Y-m-d') ."'" )
->orWhere('ANDTOBSO', null);

因此,现在对于不良部分,我尝试对列表进行排序,然后再对结果进行分页。 我已经尝试用直到今天为止我使用的原始版本替换此subQuery,它可以正常工作。

$clienti->orderBy(

    DocumentoVendita::select('MVSERIAL')
        ->whereColumn('MVCODCON', 'ANCODICE')
        ->orderBy('MVSERIAL', 'DESC')
        ->limit(1)

, $input['sort_direction']);

在此之后,我将结果进行分页并显示在视图中

$clienti = $clienti->paginate(4);

当我位于第一页时,查询就可以了(我在SQL Server中具有经典的SELECT TOP 4),但是当我导航至第二页时,我得到的结果为空。通过发条,我提取了查询,这是我的问题。

首先,我使用DB :: raw查询生成的查询和正确的查询

SELECT * FROM (
    SELECT [ANCODICE], [ANDESCRI], [ANLOCALI], [ANCODFIS], [ANPARIVA], 0 AS FatturatoPrecedente, 0 AS Fatturato, 
    row_number() over (ORDER BY (
        SELECT '0' 
        FROM ESPREDOC_MAST 
        WHERE MVCODCON = ANCODICE 
        AND (MVCLADOC = 'FA' OR MVCLADOC = 'NC') 
        AND MVFLVEAC = 'V' 
        AND convert(varchar, MVDATDOC, 20) >= convert(varchar, '2018-01-01 00:00:00', 20) 
        AND convert(varchar, MVDATDOC, 20) <= convert(varchar, '2018-09-16 24:00:00', 20) ) DESC) as row_num 
    FROM [ESPRECONTI] 
    WHERE (format(ANDTOBSO, 'yyyy-MM-dd hh:mm:ss') >= '2019-09-16' or [ANDTOBSO] IS NULL) 
    and [ANTIPCON] = 'C' 
    GROUP BY [ANCODICE], [ANDESCRI], [ANLOCALI], [ANCODFIS], [ANPARIVA]
) as temp_table 
WHERE row_num between 5 and 8 
ORDER BY row_num

现在是用SubQuery生成的那个

SELECT * FROM (
    SELECT [ANCODICE], [ANDESCRI], [ANLOCALI], [ANCODFIS], [ANPARIVA], 0 AS FatturatoPrecedente, 0 AS Fatturato, 
    row_number() over (ORDER BY (
        SELECT '0' 
        FROM [ESPREDOC_MAST] 
        WHERE [MVCODCON] = [ANCODICE] 
        and convert(varchar, MVDATDOC, 20) >= convert(varchar, '2018-01-01 00:00:00', 20) 
        and convert(varchar, MVDATDOC, 20) <= convert(varchar, '2018-09-16 24:00:00', 20) 
        and [MVFLVEAC] = 'C' 
        and ([MVCLADOC] = 'V' or [MVCLADOC] = 'FA')) DESC) as row_num 
    FROM [ESPRECONTI] 
    WHERE (format(ANDTOBSO, 'yyyy-MM-dd hh:mm:ss') >= '2019-09-16' or [ANDTOBSO] IS NULL) 
    and [ANTIPCON] = 'NC' 
    GROUP BY [ANCODICE], [ANDESCRI], [ANLOCALI], [ANCODFIS], [ANPARIVA]
) as temp_table 
WHERE row_num between 5 and 8 
ORDER BY row_num

如您所见,绑定顺序错误,在第一个查询(已更正的查询)中:

“ FA”,“ NC”,“ V”,“ C”

第二个是:

'C','V','FA','NC'

这就像将全局范围附加到了错误的模型上,或者是混合的或类似的东西。

我做错什么了吗?有什么我可以尝试或检查的吗?

预先感谢任何人。

0 个答案:

没有答案