Exemplo n.º 1
0
 /**
  * Count the number of rows matched
  * @param  array  $where Any where clauses to be used in selection
  * @return int           Number of rows selected
  */
 protected function _count($where = array())
 {
     $query = Database::query()->from($this->table);
     foreach ($where as $clause => $value) {
         $query->where($clause, $value);
     }
     return $query->count();
 }
Exemplo n.º 2
0
 /**
  * Validate that a given field value exists in a given database table
  * @param  string $field     Field being validated
  * @param  mixed $value      Value being validated
  * @param  array $parameter  Optional column name
  * @return bool
  */
 protected function _validateExists($field, $value, $parameters)
 {
     // If column name does not match field name, it can be specified
     // in the second parameter position, after the table name
     if (isset($parameters[0])) {
         $attribute = $parameters[0];
     }
     // Count the number of elements to be looked for.
     // If array count all values in array else count equals 1
     if (is_array($value)) {
         $count = count($value);
     } else {
         $count = 1;
     }
     $query = Database::query()->from($attribute);
     // If given value was an array check for all values in array
     // otherwise check for single given value in the table
     if (is_array($value)) {
         $query = $query->whereIn($field, $value);
     } else {
         $query = $query->where($field, '=', $value);
     }
     return $query->count()->num >= $count;
 }