title: CakePHP 3: How to display sum of values in column from finder results
body: <pre><code>CakePHP: 3.1.4
  </code></pre>
  
  <p>I have a custom finder function in my table that gets all records from this month with the row "fee" holding a decimal value.</p>
  
  <p>In a view I want to display the sum of fee values from this selection with a variable. I tried to figure it out with help of the book <a href="http://book.cakephp.org/3.0/en/orm/query-builder.html" rel="nofollow">Using SQL Functions</a>
  but I don't understand how to use the syntax correctly in my case.</p>
  
  <p>Custom finder in Table:</p>
  
  <pre class="lang-php prettyprint-override"><code>class TicketsTable extends Table
  {
      // find all tickets created this month
      public function findThismonths(Query $query, array $options){
  
      // get first and last day of month
      $first_day_this_month = date('m-01-Y');
      $last_day_this_month  = date('m-t-Y');
  
      $query
      -&gt;select(['id', 'created', 'fee', 'extend_fee'])
      -&gt;where([ function($exp) {
          $first_day_this_month = date('Y-m-01'); 
          $last_day_this_month  = date('Y-m-t');
          return $exp-&gt;between('Tickets.created', $first_day_this_month, $last_day_this_month, 'date');
      }]);
  
      return $query;
  }
  </code></pre>
  
  <p>I can easily get the count of records but I don't understand this works with a sum.</p>
  
  <p>Controller: </p>
  
  <pre class="lang-php prettyprint-override"><code>class CashpositionsController extends AppController
  {
  public function overview()
  {
  
      $tickets = TableRegistry::get('Tickets');
      $thismonths = $tickets-&gt;find('thismonths'); // get records with finder
      $thismonths_count = $thismonths-&gt;count();   // get count of records
  
      // that's what I want but this syntax does not exist in Cake...
      $thismonths_sum = $thismonths-&gt;sum('fee'); // *nope*
  
      // set to display in view 
      $this-&gt;set('thismonths_count', $thismonths_count);
      $this-&gt;set('thismonths_sum', $thismonths_sum);
  </code></pre>
  
  <p>Then in the view:</p>
  
  <pre class="lang-php prettyprint-override"><code>&lt;tr&gt;
      &lt;td&gt;Number of tickets:&lt;/td&gt;
      &lt;td&gt;&lt;?= $thismonths_count ?&gt;&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
      &lt;td&gt;Sum of cash:&lt;/td&gt;
      &lt;td&gt;&lt;?= $thismonth_sum ?&gt;&lt;/td&gt;
  &lt;/tr&gt;
  
  &lt;?php foreach ($thismonths as $ticket): ?&gt;
  &lt;tr&gt;
      &lt;td&gt;&lt;?= h($ticket-&gt;id) ?&gt; &lt;/td&gt;
      &lt;td&gt;&lt;?= h($ticket-&gt;created) ?&gt; &lt;/td&gt;
      &lt;td&gt;&lt;?= h($ticket-&gt;fee) ?&gt;&lt;/td&gt;
  &lt;/tr&gt;
  &lt;?php endforeach; ?&gt;
  </code></pre>
  
  <p>In the book there is this example:</p>
  
  <pre class="lang-php prettyprint-override"><code>// Results in SELECT COUNT(*) count FROM ...
  $query = $articles-&gt;find();
  $query-&gt;select(['count' =&gt; $query-&gt;func()-&gt;count('*')]);
  </code></pre>
  
  <p>But I can't get sth like this to work for me either with sum().</p>