src/Koldy/Db/Model.php
public static function isUnique(
      string $field,
      $value,
      $exceptionValue = null,
      string $exceptionField = null
    ): bool {
        $select = static::select();
        $select->field('COUNT(*)', 'total')->where($field, $value);

        if ($exceptionValue !== null) {
            if ($exceptionField === null) {
                $exceptionField = $field;
            }

            $select->where($exceptionField, '!=', $exceptionValue);
        }

        $results = $select->fetchAll();

        if (isset($results[0])) {
            return ($results[0]['total'] == 0);
        }

        return true;
    }
public static function isUnique(
      string $field,
      $value,
      $exceptionValue = null,
      string $exceptionField = null
    ): bool {
        $select = static::select();
        $select->field('COUNT(*)', 'total')->where($field, $value);

        if ($exceptionValue !== null) {
            if ($exceptionField === null) {
                $exceptionField = $field;
            }

            $select->where($exceptionField, '!=', $exceptionValue);
        }

        $results = $select->fetchAll();

        if (isset($results[0])) {
            return ($results[0]['total'] == 0);
        }

        return true;
    }
public static function isUnique ( string $ field , $ value , $ exceptionValue = null , string $ exceptionField = null ) : bool { $ select = static :: select ( ) ; $ select -> field ( 'COUNT(*)' , 'total' ) -> where ( $ field , $ value ) ; if ( $ exceptionValue !== null ) { if ( $ exceptionField === null ) { $ exceptionField = $ field ; } $ select -> where ( $ exceptionField , '!=' , $ exceptionValue ) ; } $ results = $ select -> fetchAll ( ) ; if ( isset ( $ results [ 0 ] ) ) { return ( $ results [ 0 ] [ 'total' ] == 0 ) ; } return true ; }
Check if some value exists in database or not. This is useful if you
want, for an example, check if user's e-mail already is in database
before you try to insert your data.

@param  string $field
@param  mixed $value
@param  mixed $exceptionValue OPTIONAL
@param  string $exceptionField OPTIONAL

@return bool
@throws Query\Exception
@throws \Koldy\Exception
@link http://koldy.net/docs/database/models#isUnique

@example
User::isUnique('email', 'email@domain.com'); will execute:
SELECT COUNT(*) FROM user WHERE email = 'email@domain.com'

User::isUnique('email', 'email@domain.com', 'other@mail.com');
SELECT COUNT(*) FROM user WHERE email = 'email@domain.com' AND email != 'other@mail.com'

User::isUnique('email', 'email@domain.com', 5, 'id');
SELECT COUNT(*) FROM user WHERE email = 'email@domain.com' AND id != 5
Check if some value exists in database or not . This is useful if you want for an example check if user s e - mail already is in database before you try to insert your data .