对象列表查询的子条件,顺序为 "and" 和 "or"
Subconditions for objectlist query with order for "and" and "or"
我想用几个参数过滤结果:布尔状态、类别和将在标题或描述(本地化字段)上搜索的关键字。
我是新手。当我省略描述关键字条件时,我成功地使查询工作:
$advertList = new Object\Advert\Listing();
$advertList->setOrderKey("date");
$advertList->setOrder("DESC");
$conditions = [];
if ($this->getParam("keyword"))
{
$conditions[] = " title LIKE " . $advertList->quote("%" . $this->getParam("keyword") . "%");
}
if ($this->getParam("reqoffer")) {
$conditions[] = "reqoffer LIKE " . $advertList->quote("%" . $this->getParam("reqoffer") . "%");
}
if($this->getParam("category")) {
$conditions[] = "category__id = " . $this->getParam("category");
}
$advertList->setCondition(implode(" AND ", $conditions));
但是当我想添加我的 "or condtion" :
$conditions[] = "( title LIKE " . $advertList->quote("%" . $this->getParam("keyword") . "%") .
// " OR shorttext LIKE " . $advertList->quote("%" . $this->getParam("keyword") . "% )");
我无法成功对 "OR" 括号中的关键字进行首次查询:我的意思是最后,即使类别或 reqoffer 错误,只要标题正确,我也会得到结果...
我找了好几个小时,尝试了很多东西。我找不到同样的问题。我相信这对你们中的一些人来说是显而易见的。如果可以的话请帮忙。
提前致谢。
如果你想建立像 WHERE (a AND b) OR c
这样的条件,你需要做这样的事情(伪代码):
$or_conditions = [];
$and_conditions = [];
if (needA)
$and_conditions[] = "a";
if (needB)
$and_conditions[] = "b";
$or_conditions[] = "(" . implode(" AND ", $and_conditions) . ")";
$advertList->setCondition(implode(" OR ", $or_conditions));
我想用几个参数过滤结果:布尔状态、类别和将在标题或描述(本地化字段)上搜索的关键字。
我是新手。当我省略描述关键字条件时,我成功地使查询工作:
$advertList = new Object\Advert\Listing();
$advertList->setOrderKey("date");
$advertList->setOrder("DESC");
$conditions = [];
if ($this->getParam("keyword"))
{
$conditions[] = " title LIKE " . $advertList->quote("%" . $this->getParam("keyword") . "%");
}
if ($this->getParam("reqoffer")) {
$conditions[] = "reqoffer LIKE " . $advertList->quote("%" . $this->getParam("reqoffer") . "%");
}
if($this->getParam("category")) {
$conditions[] = "category__id = " . $this->getParam("category");
}
$advertList->setCondition(implode(" AND ", $conditions));
但是当我想添加我的 "or condtion" :
$conditions[] = "( title LIKE " . $advertList->quote("%" . $this->getParam("keyword") . "%") .
// " OR shorttext LIKE " . $advertList->quote("%" . $this->getParam("keyword") . "% )");
我无法成功对 "OR" 括号中的关键字进行首次查询:我的意思是最后,即使类别或 reqoffer 错误,只要标题正确,我也会得到结果...
我找了好几个小时,尝试了很多东西。我找不到同样的问题。我相信这对你们中的一些人来说是显而易见的。如果可以的话请帮忙。
提前致谢。
如果你想建立像 WHERE (a AND b) OR c
这样的条件,你需要做这样的事情(伪代码):
$or_conditions = [];
$and_conditions = [];
if (needA)
$and_conditions[] = "a";
if (needB)
$and_conditions[] = "b";
$or_conditions[] = "(" . implode(" AND ", $and_conditions) . ")";
$advertList->setCondition(implode(" OR ", $or_conditions));