public function valueExists($field, $value = null)
    {
        $record = new static($this->table, $this->keys, $this->mapper);

        if ($value === null) {
            $value = $this->$field;
        }

        try {
            $record->load([$field => $value]);
        } catch (NoResultException $e) {
            return false;
        }

        if ($record->$field != $value) {
            return false;
        }

        // check record keys same as self
        $same = [];

        foreach ($this->keys as $key) {
            $same[] = ($record->$key == $this->$key);
        }

        // Key not same, means same value exists in other record.
        if (in_array(false, $same, true)) {
            return true;
        }

        return false;
    }
public function valueExists($field, $value = null)
    {
        $record = new static($this->table, $this->keys, $this->mapper);

        if ($value === null) {
            $value = $this->$field;
        }

        try {
            $record->load([$field => $value]);
        } catch (NoResultException $e) {
            return false;
        }

        if ($record->$field != $value) {
            return false;
        }

        // check record keys same as self
        $same = [];

        foreach ($this->keys as $key) {
            $same[] = ($record->$key == $this->$key);
        }

        // Key not same, means same value exists in other record.
        if (in_array(false, $same, true)) {
            return true;
        }

        return false;
    }
public function valueExists ( $ field , $ value = null ) { $ record = new static ( $ this -> table , $ this -> keys , $ this -> mapper ) ; if ( $ value === null ) { $ value = $ this -> $ field ; } try { $ record -> load ( [ $ field => $ value ] ) ; } catch ( NoResultException $ e ) { return false ; } if ( $ record -> $ field != $ value ) { return false ; } // check record keys same as self $ same = [ ] ; foreach ( $ this -> keys as $ key ) { $ same [ ] = ( $ record -> $ key == $ this -> $ key ) ; } // Key not same, means same value exists in other record. if ( in_array ( false , $ same , true ) ) { return true ; } return false ; }
Check a field value exists in database or not, to keep a field unique.

@param   string $field The field name to check.
@param   mixed  $value The value to check.

@return bool

@throws \Exception
Check a field value exists in database or not to keep a field unique .