CRUDlex\AbstractData::countBy PHP Method

countBy() abstract public method

Retrieves the amount of entities in the datasource fulfilling the given parameters.
abstract public countBy ( string $table, array $params, array $paramsOperators, boolean $excludeDeleted ) : integer
$table string the table to count in
$params array an array with the field names as keys and field values as values
$paramsOperators array the operators of the parameters like "=" defining the full condition of the field
$excludeDeleted boolean false, if soft deleted entries in the datasource should be counted, too
return integer the count fulfilling the given parameters
    public abstract function countBy($table, array $params, array $paramsOperators, $excludeDeleted);

Usage Example

Beispiel #1
0
 /**
  * Performs the regular unique validation.
  *
  * @param $value
  * the value to validate
  * @param $data
  * the data instance to validate with
  * @param $entity
  * the entity of the field
  * @param $field
  * the field to validate
  *
  * @return boolean
  * true if everything is valid
  */
 protected function isValidUnique($value, AbstractData $data, Entity $entity, $field)
 {
     $params = [$field => $value];
     $paramsOperators = [$field => '='];
     if ($entity->get('id') !== null) {
         $params['id'] = $entity->get('id');
         $paramsOperators['id'] = '!=';
     }
     $amount = intval($data->countBy($data->getDefinition()->getTable(), $params, $paramsOperators, true));
     return $amount == 0;
 }