嗨,我正在使用doctrine查询构建器来执行查询..
这是我的询问....
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'
无论如何,你的语法令人困惑,你为什么不这样做
->where('sp.name IS NULL AND p.name LIKE :val AND sp.name LIKE :val')
在任何情况下,Doctrine都没有内置的“if”功能,所以如果你需要特定的东西,你需要自己编程。