src/QueryParserCustomFields.php
private function prepareCustomSql(array $searchCriteria, Model $modules, string $classname, string $andOr, int $fKey): string
    {
        $sql = '';
        list($searchField, $operator, $searchValue) = $searchCriteria;
        $operator = $this->operators[$operator];

        if (trim($searchValue) !== '') {
            $customFields = CustomFields::findFirst([
                'modules_id = ?0 AND name = ?1',
                'bind' => [$modules->id, $searchField],
            ]);

            $customFieldValue = $classname . '.value';
            if ($customFields->type->name == 'number') {
                $customFieldValue = 'CAST(' . $customFieldValue . ' AS INT)';
            }

            $sql .= ' AND ' . $classname . '.custom_fields_id = :cfi' . $searchField;

            $this->bindParamsKeys[] = 'cfi' . $searchField;
            $this->bindParamsValues[] = $customFields->id;

            if ($searchValue == '%%') {
                $sql .= ' ' . $andOr . ' (' . $classname . '.value IS NULL OR ' . $classname . '.value = "")';
            } elseif ($searchValue == '$$') {
                $sql .= ' ' . $andOr . ' (' . $classname . '.value IS NOT NULL OR ' . $classname . '.value != "")';
            } else {
                if (strpos($searchValue, '|')) {
                    $searchValue = explode('|', $searchValue);
                } else {
                    $searchValue = [$searchValue];
                }

                foreach ($searchValue as $vKey => $value) {
                    if (preg_match('#^%[^%]+%|%[^%]+|[^%]+%$#i', $value)) {
                        $operator = 'LIKE';
                    }

                    if (!$vKey) {
                        $sql .= ' ' . $andOr . ' (' . $customFieldValue . ' ' . $operator . ' :cfv' . $searchField . $fKey . $vKey;
                    } else {
                        $sql .= ' OR ' . $customFieldValue . ' ' . $operator . ' :cfv' . $searchField . $fKey . $vKey;
                    }

                    $this->bindParamsKeys[] = 'cfv' . $searchField . $fKey . $vKey;
                    $this->bindParamsValues[] = $value;
                }

                $sql .= ')';
            }
        }

        return $sql;
    }
private function prepareCustomSql(array $searchCriteria, Model $modules, string $classname, string $andOr, int $fKey): string
    {
        $sql = '';
        list($searchField, $operator, $searchValue) = $searchCriteria;
        $operator = $this->operators[$operator];

        if (trim($searchValue) !== '') {
            $customFields = CustomFields::findFirst([
                'modules_id = ?0 AND name = ?1',
                'bind' => [$modules->id, $searchField],
            ]);

            $customFieldValue = $classname . '.value';
            if ($customFields->type->name == 'number') {
                $customFieldValue = 'CAST(' . $customFieldValue . ' AS INT)';
            }

            $sql .= ' AND ' . $classname . '.custom_fields_id = :cfi' . $searchField;

            $this->bindParamsKeys[] = 'cfi' . $searchField;
            $this->bindParamsValues[] = $customFields->id;

            if ($searchValue == '%%') {
                $sql .= ' ' . $andOr . ' (' . $classname . '.value IS NULL OR ' . $classname . '.value = "")';
            } elseif ($searchValue == '$$') {
                $sql .= ' ' . $andOr . ' (' . $classname . '.value IS NOT NULL OR ' . $classname . '.value != "")';
            } else {
                if (strpos($searchValue, '|')) {
                    $searchValue = explode('|', $searchValue);
                } else {
                    $searchValue = [$searchValue];
                }

                foreach ($searchValue as $vKey => $value) {
                    if (preg_match('#^%[^%]+%|%[^%]+|[^%]+%$#i', $value)) {
                        $operator = 'LIKE';
                    }

                    if (!$vKey) {
                        $sql .= ' ' . $andOr . ' (' . $customFieldValue . ' ' . $operator . ' :cfv' . $searchField . $fKey . $vKey;
                    } else {
                        $sql .= ' OR ' . $customFieldValue . ' ' . $operator . ' :cfv' . $searchField . $fKey . $vKey;
                    }

                    $this->bindParamsKeys[] = 'cfv' . $searchField . $fKey . $vKey;
                    $this->bindParamsValues[] = $value;
                }

                $sql .= ')';
            }
        }

        return $sql;
    }
private function prepareCustomSql ( array $ searchCriteria , Model $ modules , string $ classname , string $ andOr , int $ fKey ) : string { $ sql = '' ; list ( $ searchField , $ operator , $ searchValue ) = $ searchCriteria ; $ operator = $ this -> operators [ $ operator ] ; if ( trim ( $ searchValue ) !== '' ) { $ customFields = CustomFields :: findFirst ( [ 'modules_id = ?0 AND name = ?1' , 'bind' => [ $ modules -> id , $ searchField ] , ] ) ; $ customFieldValue = $ classname . '.value' ; if ( $ customFields -> type -> name == 'number' ) { $ customFieldValue = 'CAST(' . $ customFieldValue . ' AS INT)' ; } $ sql .= ' AND ' . $ classname . '.custom_fields_id = :cfi' . $ searchField ; $ this -> bindParamsKeys [ ] = 'cfi' . $ searchField ; $ this -> bindParamsValues [ ] = $ customFields -> id ; if ( $ searchValue == '%%' ) { $ sql .= ' ' . $ andOr . ' (' . $ classname . '.value IS NULL OR ' . $ classname . '.value = "")' ; } elseif ( $ searchValue == '$$' ) { $ sql .= ' ' . $ andOr . ' (' . $ classname . '.value IS NOT NULL OR ' . $ classname . '.value != "")' ; } else { if ( strpos ( $ searchValue , '|' ) ) { $ searchValue = explode ( '|' , $ searchValue ) ; } else { $ searchValue = [ $ searchValue ] ; } foreach ( $ searchValue as $ vKey => $ value ) { if ( preg_match ( '#^%[^%]+%|%[^%]+|[^%]+%$#i' , $ value ) ) { $ operator = 'LIKE' ; } if ( ! $ vKey ) { $ sql .= ' ' . $ andOr . ' (' . $ customFieldValue . ' ' . $ operator . ' :cfv' . $ searchField . $ fKey . $ vKey ; } else { $ sql .= ' OR ' . $ customFieldValue . ' ' . $ operator . ' :cfv' . $ searchField . $ fKey . $ vKey ; } $ this -> bindParamsKeys [ ] = 'cfv' . $ searchField . $ fKey . $ vKey ; $ this -> bindParamsValues [ ] = $ value ; } $ sql .= ')' ; } } return $ sql ; }
Prepare the SQL for a custom fields search.

@param array $searchCriteria
@param Model $modules
@param string $classname
@param string $andOr
@param int $fKey

@return string
Prepare the SQL for a custom fields search .