2016-10-14 8 views
0

sq mileageフィールドを持つレコードがあります。これらのフィールドを日付で合計したいので、表示ページや注文などに関係なく、エンティティごとの値は同じになります。 SELECT SUM(sq_miles) FROM table WHERE date <= ($this->_properties['date']);しかしCakePHP3の新しいORMと私はコードにそれを置く方法がわからない: は、例えば:エンティティ別に仮想合計フィールドを作成するにはどうすればいいですか

 
|id| date |sqmiles|total| 
|--|----------|-------|-----| 
|1 |2010-10-10| 2 | 2 | 
|2 |2011-11-11| 3 | 5 | 
|3 |2012-12-12| 1 | 6 | 
 
|id| date |sqmiles|total| 
|--|----------|-------|-----| 
|3 |2012-12-12| 1 | 6 | 
|1 |2010-10-10| 2 | 2 | 
|2 |2011-11-11| 3 | 5 | 

私の開始の思考は、それは、次のSQLを要求する仮想フィールドでした。

答えて

0

これが「最高」の答えではないかもしれないが、私はそれが仕事作っ: エンティティでは、次の機能をuse Cake\ORM\TableRegistry;を追加します。

protected function _getTotalSqMiles() 
    { 
     $annexations = TableRegistry::get('Annexations'); 
     $query = $annexations->find(); 
     $sum = $query->func()->sum('sq_miles'); 
     $results = $query->select(['total' => $sum]) 
      ->where([ 
       'effective_date <=' => $this->_properties['effective_date'] 
      ]) 
      ->first(); 
     return $results->total; 
    } 
関連する問題