Search code examples
symfonydoctrine-query

Syntax Error - line 0, col 469: Error: Expected known function, got 'IF'


Hi I'm using doctrine query builder for execute a query..

This is my query....

return $this->createQueryBuilder('sp')
                        ->select('sp.id, sp.price, sp.offer_price AS offerPrice, sp.price_kg AS PriceKg, '
                                . 'sp.offer_price_kg AS offerPriceKg, sp.status AS status, s.name AS storename')
                        ->addSelect("(CASE WHEN (sp.name IS NULL) THEN p.name ELSE sp.name END) AS spname")
                        ->addSelect("(CASE WHEN (sp.sku IS NULL) THEN p.sku ELSE sp.sku END) AS spsku")
                        ->addSelect("(CASE WHEN (sp.main_image IS NULL) THEN p.main_image ELSE sp.main_image END) AS sp_main_image")
                        ->join('sp.store', 's')
                        ->join('sp.product', 'p')
                        ->Where('IF(sp.name IS NULL, p.name, sp.name) LIKE :val')
                        ->orWhere('sp.sku LIKE :val')
                        ->setParameter('val', '%' . $search . '%')
                        ->getQuery();

Seems like this if condition gives an error,

->Where('IF(sp.name IS NULL, p.name, sp.name) LIKE :val')

This is the error I get,

[Syntax Error] line 0, col 469: Error: Expected known function, got 'IF'


Solution

  • Your syntax is confusing anyway, why don't you do

    ->where('sp.name IS NULL AND p.name LIKE :val AND sp.name LIKE :val')
    

    In any case, Doctrine does not have a built in "if" function, so if you need something specific you're going to need to program it yourself.

    https://www.doctrine-project.org/projects/doctrine-orm/en/2.6/cookbook/dql-user-defined-functions.html