Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Doctrine Extensions REGEXP not working in Symfony2

I am using Doctrine 2.1 and need to use a REGEXP in MySQL.

REGEXP is not currently supported in the default installation of Doctrine so I am using beberlei/DoctrineExtensions

I cannot get Doctrine to recognize my REGEXP, I have followed the example on SO.

I'm using Doctrine 2.1 and Symfony 2.7.1

Here's the code, any ideas?

Config

# config.yml
# Doctrine Configuration
doctrine:
    dbal:
        driver:   "%database_driver%"
        host:     "%database_host%"
        port:     "%database_port%"
        dbname:   "%database_name%"
        user:     "%database_user%"
        password: "%database_password%"
        charset:  UTF8
        keep_slave: true
        slaves:   %database_slaves%

    orm:
        auto_generate_proxy_classes: "%kernel.debug%"
        auto_mapping: true
        metadata_cache_driver: apc
        query_cache_driver: apc
        result_cache_driver:
            type: service
            id: cache
        dql:
            string_functions:
                regexp: DoctrineExtensions\Query\Mysql\Regexp

Repository

// GroupRepository
            $dql = "SELECT g FROM {$this->_entityName} g WHERE g.name REGEXP '^[:alpha:]'";
            return $this->getEntityManager()->createQuery($dql)->getResult();

Error on page load

// Error
CRITICAL - Uncaught PHP Exception Doctrine\ORM\Query\QueryException: "[Syntax Error] line 0, col 64: Error: Expected =, <, <=, <>, >, >=, !=, got 'REGEXP'" at /symfony/vendor/doctrine/orm/lib/Doctrine/ORM/Query/QueryException.php line 52 

UPDATE:

I've updated the query to match the expected format

    $dql = "SELECT g FROM {$this->_entityName} g WHERE REGEXP(g.name, '^[:alpha:]')";

Now I have started getting a new error:

[Syntax Error] line 0, col -1: Error: Expected =, <, <=, <>, >, >=, !=, got end of string.
like image 410
Patrick Avatar asked Oct 29 '25 18:10

Patrick


1 Answers

I found the solution through the github page for the project

Doctrine requires that all where clauses require a comparison operator even though a clause like REGEXP doesn't require it.

$dql = "SELECT g FROM {$this->_entityName} g WHERE g.status = 1 AND REGEXP(g.name, '^[^[:alpha:]]') = 1";

Also my regex character class was incorrect in my original question.

like image 140
Patrick Avatar answered Oct 31 '25 08:10

Patrick



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!