しょうが無いので、Zend_DB_Selectを修正。
<?php // before public function where($cond, $value = null, $type = null) { $this->_parts[self::WHERE][] = $this->_where($cond, $value, $type, true); return $this; } // after public function where($cond, $value = null, $type = null) { // if $cond is null then ignore. if ((is_null($cond) || ($cond == '')) == false) { $this->_parts[self::WHERE][] = $this->_where($cond, $value, $type, true); } return $this; } public function orWhere($cond, $value = null, $type = null) { // if $cond is null then ignore. if ((is_null($cond) || ($cond == '')) == false) { $this->_parts[self::WHERE][] = $this->_where($cond, $value, $type, false); } return $this; }
これでwhere()やorWhere()にnullか空白文字列渡しても無視してくれるようになったので、SqlHelperのmakeWhere()も一応、空白を返す場合はnullにするように変更。
<?php public function makeWhere() { if (count($this->_whereArray) == 0) { return null; } $res = null; foreach ($this->_whereArray as $item) { if ($res == null) { $res = '(' . $item['value'] . ')'; } else { $res = $res . ' ' . $item['cond'] . ' (' . $item['value'] . ')'; } } return $res; } // ついでに、makeWhereの結果は自分自身にも渡されるので、こっちでもnullと空白を無視 public function addOr($value) { if((is_null($value) || ($value == '')) == false) { $this->_whereArray[] = array('cond' => 'or', 'value' => $value); } } public function addAnd($value) { if((is_null($value) || ($value == '')) == false) { $this->_whereArray[] = array('cond' => 'and', 'value' => $value); } }
これで無事(?)、気兼ねなく条件分岐なしで呼び出し可能になりましたよと。
あ、戻り値の$resを()でくくらないのは、where()に渡そうがaddOr()/addAnd()に渡そうがその中で必ずくくられるからです。
別に多重にくくっても問題は無いですが、生成されたSQL文を見ると括弧が冗長すぎて気持ち悪くなるですよ‥‥