Example #1
0
 public function preFind(Event $event, Query $query, $finder)
 {
     if ($finder === 'first') {
         return ['custom' => 'data'];
     } else {
         if ($finder === 'all') {
             $query->fields('id', 'username');
         } else {
             if ($finder === 'list') {
                 return false;
             }
         }
     }
     return true;
 }
Example #2
0
 /**
  * Perform an aggregation on the database and return the calculated value.
  * The currently supported aggregates are `avg`, `count`, `min`, `max`, and `sum`.
  *
  * @param \Titon\Db\Query $query
  * @param string $function
  * @param string $field
  * @return int
  */
 public function aggregate(Query $query, $function, $field)
 {
     $query->fields(Query::func(strtoupper($function), [$field => Func::FIELD])->asAlias('aggregate'));
     $results = $this->getDriver()->setContext('read')->executeQuery($query)->find();
     if (isset($results[0])) {
         return (int) $results[0]['aggregate'];
     }
     return 0;
 }
Example #3
0
 /**
  * @expectedException \Titon\Db\Exception\InvalidQueryException
  */
 public function testFormatFieldsThrowsErrorsNoJoinFields()
 {
     $query = new Query(Query::SELECT, new User());
     $query->fields(['id', 'country_id', 'username']);
     $query->leftJoin(['countries', 'Country'], [], ['users.country_id' => 'Country.id']);
     $this->object->formatFields($query);
 }