语法错误-第0行,列469:错误:预期的已知函数,得到了'IF'

时间:2018-08-17 09:06:38

标签: symfony doctrine-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();

如果条件给出错误,则看起来像这样

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

这是我得到的错误,

  

[语法错误]行0,列469:错误:预期的已知函数,得到了   'IF'

1 个答案:

答案 0 :(得分:1)

您的语法仍然令人困惑,为什么不这样做

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

无论如何,Doctrine没有内置的“ if”功能,因此,如果您需要特定的东西,则需要自己进行编程。

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