user7722025
user7722025

Reputation:

How to use substring in a querybuilder where clause

How to use substring in querybuilder :

I tried this:

$qb->andWhere($qb->expr()->substring('t0.country',1,2) != 'FR');

...

but i have this exception:

Warning: get_class() expects parameter 1 to be object, boolean given

Upvotes: 3

Views: 1613

Answers (1)

miken32
miken32

Reputation: 42700

You were trying to use the != operator in PHP, which will not work. Instead, wrap the condition in the neq function, which tests for inequality.

$qb->andWhere(
    $qb->expr()->neq(
        $qb->expr()->substring('t0.country', 1, 2), 
        $qb->expr()->literal('FR')
    )
);

See documentation here: https://www.doctrine-project.org/projects/doctrine-orm/en/2.6/reference/query-builder.html#the-expr-class

Upvotes: 3

Related Questions