Example #1
0
 /**
  * 1.- Get the database connection for the model.
  * 2.- Execute a Closure within a transaction.
  * 3.- Find a model by its primary key or return new static.
  * 4.- Save the model to the database.
  *
  * Guarda todas las configuraciones proporcionadas por el usuario.
  *
  *
  * @access public
  * @param  array  $configuracion
  * @return array
  */
 public static function guardaConfiguracion(array $configuracion = array())
 {
     $self = new self();
     $conn = $self->getConnection();
     $conn->transaction(function () use($self, $configuracion) {
         foreach ($configuracion as $clave => $valor) {
             $model = $self->findOrNew($clave);
             $model->clave = $clave;
             $model->valor = $valor;
             $model->save();
         }
     });
     return $configuracion;
 }
Example #2
0
 /**
  * Allows to perform a summatory group for a column in the collection
  *
  * @param string $field
  * @param array|null $conditions
  * @param string|null $finalize
  * @return array|null
  * @throws Exception
  */
 public static function summatory($field, $conditions = null, $finalize = null)
 {
     if (is_string($field) === false) {
         throw new Exception('Invalid field name for group');
     }
     if (is_array($conditions) === false && is_null($conditions) === false) {
         throw new Exception('Invalid parameter type.');
     }
     if (is_string($finalize) === false && is_null($finalize) === false) {
         throw new Exception('Invalid parameter type.');
     }
     $model = new self();
     $connection = $model->getConnection();
     $source = $model->getSource();
     if (empty($source) === true) {
         throw new Exception('Method getSource() returns empty string');
     }
     $collection = $connection->selectCollection($source);
     /*
      * Uses a javascript hash to group the results, however this is slow with larger
      * datasets
      */
     $group = $collection->group(array(), array('summatory' => array()), "function (curr, result) { if (typeof result.summatory[curr." . $field . "] === \"undefined\") { result.summatory[curr." . $field . "] = 1; } else { result.summatory[curr." . $field . "]++; } }");
     if (isset($group['retval']) === true) {
         if (isset($group['retval'][0]) === true) {
             $firstRetval = $group['retval'][0];
             if (isset($firstRetval['summatory']) === true) {
                 return $firstRetval['summatory'];
             }
             return $firstRetval;
         }
         return $group['retval'];
     }
 }