如何使用 Zend_Db_Select 与 AND 和 OR 运算符在 Zend Framework 1 中执行嵌套 WHERE?
How to do a nested WHERE in Zend Framework 1 using Zend_Db_Select with AND and OR operators?
所以...我已经继承了这个基于 Zend Framework 1.12 的相当大的项目,我试图添加的一个功能涉及比我过去对项目所做的更复杂的数据库操作。我对 Zend Framework 或 MVC 非常陌生。我为 Zend Framework 3 找到了一个完美的答案,但我不得不使用这个版本。
该函数基本上基于各种参数构建了一个 Zend_Db_Select,我要添加的功能将涉及连接两个不同的表并检查一个或另一个中是否存在特定组合。
这是我目前的情况:
//SQL that I'm trying to do. Assume table1 and table2 are already joined.
//Ignore the imperfect syntax. I'm trying to get the concept across.
//SELECT * FROM (table1 joined to table2 by a common key)
//WHERE ( (table1.column1 = myParam1) AND (table1.column2 = myParam2) )
//OR WHERE ( (table2.column1 = myParam1) AND (table2.column2 = myParam2) )
public function buildSelect($params){
//Zend code starts here
//This one starts the Zend_Db_Select
$select = $this->select();
$table1Name = get_table_name_from_object($table1);
//lots of preexisting code here
//my code starts here.
$table2Name = get_table_name_from_object($table2);
$select->join($table2Name, "$table1Name.key = $table2Name.key", array('column1', 'column2', 'key');
//After I wrote this, I instantly realized why it won't work the way I intended it but putting it here to show what I tried at which point I got stuck.
$select->where("($table1Name.column1 = ?) OR ($table2Name.column1 = ?)",$params[1]);
$select->where( "($table1Name.column2 = ?) OR ($table2Name.column2 = ?)", $params[2]);
//more preexisting code below.
return $select
}
显然,如果我按原样尝试,程序会很高兴地 return 结果包括组合,例如,param1 在 table1.column1 中,param2 在 table2.column2 中的条目。
我从朋友那里收到了一些反馈并张贴在这里以供后代使用。
他们注意到我的代码已经包含括号并建议我简单地利用 orWhere() 然后这样写:
$select->where("($tableName1.column1 = ?", $params[param1])
->where("$tableName1.column2 = ?)", $params[param2]);
$select->orWhere("($tableName1.column1 = ?",$params[param1])
->where("$tableName2.column2 = ?)",$params[param2]);
所以...我已经继承了这个基于 Zend Framework 1.12 的相当大的项目,我试图添加的一个功能涉及比我过去对项目所做的更复杂的数据库操作。我对 Zend Framework 或 MVC 非常陌生。我为 Zend Framework 3 找到了一个完美的答案,但我不得不使用这个版本。
该函数基本上基于各种参数构建了一个 Zend_Db_Select,我要添加的功能将涉及连接两个不同的表并检查一个或另一个中是否存在特定组合。
这是我目前的情况:
//SQL that I'm trying to do. Assume table1 and table2 are already joined.
//Ignore the imperfect syntax. I'm trying to get the concept across.
//SELECT * FROM (table1 joined to table2 by a common key)
//WHERE ( (table1.column1 = myParam1) AND (table1.column2 = myParam2) )
//OR WHERE ( (table2.column1 = myParam1) AND (table2.column2 = myParam2) )
public function buildSelect($params){
//Zend code starts here
//This one starts the Zend_Db_Select
$select = $this->select();
$table1Name = get_table_name_from_object($table1);
//lots of preexisting code here
//my code starts here.
$table2Name = get_table_name_from_object($table2);
$select->join($table2Name, "$table1Name.key = $table2Name.key", array('column1', 'column2', 'key');
//After I wrote this, I instantly realized why it won't work the way I intended it but putting it here to show what I tried at which point I got stuck.
$select->where("($table1Name.column1 = ?) OR ($table2Name.column1 = ?)",$params[1]);
$select->where( "($table1Name.column2 = ?) OR ($table2Name.column2 = ?)", $params[2]);
//more preexisting code below.
return $select
}
显然,如果我按原样尝试,程序会很高兴地 return 结果包括组合,例如,param1 在 table1.column1 中,param2 在 table2.column2 中的条目。
我从朋友那里收到了一些反馈并张贴在这里以供后代使用。
他们注意到我的代码已经包含括号并建议我简单地利用 orWhere() 然后这样写:
$select->where("($tableName1.column1 = ?", $params[param1])
->where("$tableName1.column2 = ?)", $params[param2]);
$select->orWhere("($tableName1.column1 = ?",$params[param1])
->where("$tableName2.column2 = ?)",$params[param2]);