main/app/API/ValidatorProvider.php
private function validateUnique(array $uniqueFields, array $data, $mode, $class)
    {
        $errors = [];

        foreach ($uniqueFields as $dataProp => $entityProp) {
            if (isset($data[$dataProp])) {
                $qb = $this->om->createQueryBuilder();

                $qb->select('DISTINCT o')
               ->from($class, 'o')
               ->where("o.{$entityProp} LIKE :{$entityProp}")
               ->setParameter($entityProp, $data[$dataProp]);

                if (self::UPDATE === $mode && isset($data['id'])) {
                    $parameter = is_numeric($data['id']) ? 'id' : 'uuid';
                    $value = is_numeric($data['id']) ? (int) $data['id'] : $data['id'];
                    $qb->setParameter($parameter, $value)->andWhere("o.{$parameter} != :{$parameter}");
                }

                $objects = $qb->getQuery()->getResult();

                if ((self::UPDATE === $mode && isset($data['id'])) || self::CREATE === $mode) {
                    if (count($objects) > 0) {
                        $errors[] = ['path' => $dataProp, 'message' => "{$entityProp} already exists and should be unique"];
                    }
                } else {
                    if (count($objects) > 1) {
                        $errors[] = ['path' => $dataProp, 'message' => "{$entityProp} already exists and should be unique"];
                    }
                }
            }
        }

        return $errors;
    }
private function validateUnique(array $uniqueFields, array $data, $mode, $class)
    {
        $errors = [];

        foreach ($uniqueFields as $dataProp => $entityProp) {
            if (isset($data[$dataProp])) {
                $qb = $this->om->createQueryBuilder();

                $qb->select('DISTINCT o')
               ->from($class, 'o')
               ->where("o.{$entityProp} LIKE :{$entityProp}")
               ->setParameter($entityProp, $data[$dataProp]);

                if (self::UPDATE === $mode && isset($data['id'])) {
                    $parameter = is_numeric($data['id']) ? 'id' : 'uuid';
                    $value = is_numeric($data['id']) ? (int) $data['id'] : $data['id'];
                    $qb->setParameter($parameter, $value)->andWhere("o.{$parameter} != :{$parameter}");
                }

                $objects = $qb->getQuery()->getResult();

                if ((self::UPDATE === $mode && isset($data['id'])) || self::CREATE === $mode) {
                    if (count($objects) > 0) {
                        $errors[] = ['path' => $dataProp, 'message' => "{$entityProp} already exists and should be unique"];
                    }
                } else {
                    if (count($objects) > 1) {
                        $errors[] = ['path' => $dataProp, 'message' => "{$entityProp} already exists and should be unique"];
                    }
                }
            }
        }

        return $errors;
    }
private function validateUnique ( array $ uniqueFields , array $ data , $ mode , $ class ) { $ errors = [ ] ; foreach ( $ uniqueFields as $ dataProp => $ entityProp ) { if ( isset ( $ data [ $ dataProp ] ) ) { $ qb = $ this -> om -> createQueryBuilder ( ) ; $ qb -> select ( 'DISTINCT o' ) -> from ( $ class , 'o' ) -> where ( "o.{$entityProp} LIKE :{$entityProp}" ) -> setParameter ( $ entityProp , $ data [ $ dataProp ] ) ; if ( self :: UPDATE === $ mode && isset ( $ data [ 'id' ] ) ) { $ parameter = is_numeric ( $ data [ 'id' ] ) ? 'id' : 'uuid' ; $ value = is_numeric ( $ data [ 'id' ] ) ? ( int ) $ data [ 'id' ] : $ data [ 'id' ] ; $ qb -> setParameter ( $ parameter , $ value ) -> andWhere ( "o.{$parameter} != :{$parameter}" ) ; } $ objects = $ qb -> getQuery ( ) -> getResult ( ) ; if ( ( self :: UPDATE === $ mode && isset ( $ data [ 'id' ] ) ) || self :: CREATE === $ mode ) { if ( count ( $ objects ) > 0 ) { $ errors [ ] = [ 'path' => $ dataProp , 'message' => "{$entityProp} already exists and should be unique" ] ; } } else { if ( count ( $ objects ) > 1 ) { $ errors [ ] = [ 'path' => $ dataProp , 'message' => "{$entityProp} already exists and should be unique" ] ; } } } } return $ errors ; }
only if uniqueFields in data
only if uniqueFields in data