0

영어로 죄송합니다.하지만 저를 이해할 수 있기를 바랍니다.CakePHP 3 - 페이지 매김 - 계산 된 필드를 정렬하는 방법?

필드 availability은 데이터베이스에 존재하지 않습니다. 나중에 formatResults에서 생성되었습니다. 결과는 으로 올바르게 표시되지만 availability 필드로는 정렬 할 수 없습니다.

나는이 방법을 시도했지만 작동하지 않습니다

$query = $this 
    ->WebshopProducts 
    ->find('all') 
    -> 
    ->formatResults(function($results) { 
     return $results->map(function($row) { 
      if($row->stock_total - $row->stock_min > 0){ 
       $row->availability='Yes'; 
      }else{ 
       $row->availability='No'; 
      } 
      return $row; 
     }); 
    }); 
+0

'$ this-> = [ 'sortWhitelist'=> [ '가용성']] 페이지를 매기,' – dype

답변

0
$query = $this 
->WebshopProducts 
->find('all') 
->select('Availibility'=>'(WebshopProducts.stock_total - WebshopProducts.stock_min)', **(Other required fields)**) 
->order('Availibility'=>"DESC") 
->formatResults(function($results) { 
    return $results->map(function($row) { 
     if($row->stock_total - $row->stock_min > 0){ 
      $row->availability='Yes'; 
     }else{ 
      $row->availability='No'; 
     } 
     return $row; 
    }); 
}); 

을보기 파일에 내가 확실하지 않다

<th><?php echo $this->Paginator->sort('Availibility'); ?></th> 

해야하지만 당신이 시도 할 수 있습니다.

+0

좋은 솔루션을 당신에게 Haresh 감사드립니다. 나는 다른 방식으로 문제를 해결했다. 나의 해결책은 아래에있다. – lupi

0

해결책을 찾았습니다. 이 5 월 도움이 사람 :

$query = $this->WebshopProducts->find('all'); 
      $availabilityCase = $query->newExpr()->addCase(
      [$query->newExpr()->add(['(stock_total - stock_min) >' => 0]),$query->newExpr()->add(['(stock_total - stock_min) <=' => 0])],['YES','NO'],['string','string']); 
      $query->select(['id','active','title','price','stock_total','position','availability' => $availabilityCase]);   
     $this->set('webshopProducts', $this->paginate($query)); 
관련 문제