2016-07-16 22 views
0

Entschuldigung für mein Englisch, aber ich hoffe, Sie werden mich verstehen.CakePHP 3 - Paginierung - wie berechnet man das berechnete Feld?

Feld availability existiert nicht in der Datenbank. Es wurde anschließend in formatResults erstellt. Die Ergebnisse werden korrekt angezeigt, , aber es ist nicht möglich, nach dem Feld availability zu sortieren.

habe ich versucht, auf diese Weise, aber es funktioniert nicht:

$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-> Paginieren = [ 'sortWhitelist' => [ 'Verfügbarkeit']];' – dype

Antwort

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; 
    }); 
}); 

Ihrer Ansicht Datei sollte es

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

Ich bin nicht sicher sein, aber Sie können dies versuchen.

+0

Danke Haresh für eine gute Lösung. Ich löste das Problem auf eine andere Weise. Meine Lösung ist unten – lupi

0

Ich habe eine Lösung gefunden. Dies kann hilft jemand:

$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));