We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Criteria::fromInput issue on searching textfields

Hello,

Criteria::fromInput is a cool feature for searching records in your db, When I try to search in a varchar field, it will return a LIKE statement (i.e. username LIKE :username:) When I try to search in a text field it will return a normal = statement (i.e. description = :description:)

In the phalcon documentation (explaining INVO) on url: https://docs.phalcon.io/en/latest/reference/tutorial-invo.html It says:

If the field data type is text or similar (char, varchar, text, etc.) It uses an SQL “like” operator to filter the results. If the data type is not text or similar, it’ll use the operator “=”.

My question:

Is this a bug, or do I forgot something?

$query = Criteria::fromInput($this->di,"Categories",$criteria);
$extra = $this->modelsManager->createBuilder($query->getParams());
$this->persistent->searchParams=$query->getParams();


98.9k

No, this is not a bug, this is the expected behavior, if you want to implement a custom similar function you can take this as example:

    public static function fromInput($dependencyInjector, $modelName, $data)
    {

        $conditions = array();
        if (count($data)) {

            $metaData = $dependencyInjector->getShared('modelsMetadata');

            $model = new $modelName();
            $dataTypes = $metaData->getDataTypes($model);
            $columnMap = $metaData->getReverseColumnMap($model);

            $bind = array();

            foreach ($data as $fieldName => $value) {

                if (isset($columnMap[$fieldName])) {
                    $field = $columnMap[$fieldName];
                } else {
                    continue;
                }

                if (isset($dataTypes[$field])) {

                    if (!is_null($value)) {
                        if ($value != '') {                         
                            if ($dataTypes[$field] == 2) {                              
                                $condition = $fieldName . " LIKE :" . $fieldName . ":";                             
                                $bind[$fieldName] = '%' . $value . '%';
                            } else {                                
                                $condition = $fieldName . ' = :' . $fieldName . ':';
                                $bind[$fieldName] = $value;
                            }
                            $conditions[] = $condition;
                        }
                    }
                }
            }
        }

        $criteria = new Criteria();
        if (count($conditions)) {           
            $criteria->where(join(' AND ', $conditions));
            $criteria->bind($bind);
        }

        return $criteria;

    }


506

I am getting the same issue, I am not sure why this would be expected behavior.. for text fields it is searching with an "=" instead of a "LIKE".