嗨,我正在使用主义查询构建器来执行查询。
这是我的查询。...
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'
答案 0 :(得分:1)
您的语法仍然令人困惑,为什么不这样做
->where('sp.name IS NULL AND p.name LIKE :val AND sp.name LIKE :val')
无论如何,Doctrine没有内置的“ if”功能,因此,如果您需要特定的东西,则需要自己进行编程。