CakePHP 3.6.17:排序快速加载

时间:2019-01-07 14:08:48

标签: php sorting cakephp eager-loading model-associations

我想在cakephp 3.6中将紧急加载与排序方式结合起来

在我的控制器中,我可以使用紧急加载或排序方式,但不能将它们组合在一起。这是我的代码:

急于加载:

    $user = $this->Users->get($id, [
        'contain' => [ 'TasksTo' => ['ProjectStatus']]
    ]); 
    $this->set(compact('user'));

排序:

    $user = $this->Users->get($id, [
        'contain' => ['TasksTo'  => ['sort' => ['TasksTo.priority' => 'ASC']]]); 
    $this->set(compact('user'));
}

当单独使用但不能一起使用时,两者都能按预期工作。我如何将它们结合起来?

2 个答案:

答案 0 :(得分:1)

使用点符号。假设ProjectStatus已链接到TasksTo,请尝试以下操作:

$user = $this->Users->get($id, [
    'contain' => ['TasksTo.ProjectStatus' => ['sort' => ['TasksTo.priority' => 'ASC']]]); 
$this->set(compact('user'));

答案 1 :(得分:1)

好吧,我这样修复它:

    ;with cte
    AS
    (
        Select ROW_NUMBER() OVER(partition by STAT Order by UDATE ) as Rn,  
        *,
        LAG(UDATE) OVER(partition by STAT Order by UDATE ) As PrevUDate,
        COUNT(*) OVER(partition by STAT) As [Count]
        from YourTable
    )



    Select Max(rn) As [Count],
    OBJNR,STAT,
    SUM(CASE WHEN rn%2=0 THEN DATEDIFF(d,PrevUDate,UDATE) 
            WHEN rn=[Count] THEN  DATEDIFF(d,UDATE,getDate())   
        ELSE 0 END)
    from cte
    Group BY OBJNR, STAT