so I’m using this program http://code.google.com/p/ecartcommerce/source/browse/library/Ecart/Db/Table/Select/Disassemble.php?edit=1 to generate a string that corresponds to the zend db select command for creating that query..
but the thing screws up when I use an IN() statement in WHERE
so suppose I have
"SELECT * FROM j WHERE id IN (1,2,3,5,6)";
and I convert this using that class,
when I inspected the output, it stripped out the parentheses between the numbers in the IN clause, so it became
->where("id IN 1,2,3,4,5,6"), while it’s supposed to be ->where("id IN (1,2,3,4,5,6)")
Anyone knows how to fix this?
I suspect it has something to do with the _addWhere method:
protected function _addWhere($where)
{
$result = '';
$where = 'AND ' . $where;
$replacement = true;
while ($replacement) {
$replacement = false;
$parts = preg_split(
'/(\(.+\)+)/U', $where, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE
);
foreach ($parts as $part) {
if (!preg_match('/\(.+?\)/', $part)) {
continue;
}
if (strstr($part, ' AND ') || strstr($part, ' OR ')) {
continue;
}
$replacement = preg_replace('/^\s*\((.+)\)\s*$/', '$1', $part);
$where = str_replace($part, $replacement, $where);
}
}
$parts = preg_split(
'/(\(.*\))/U', $where, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE
);
foreach ($parts as $part) {
if (preg_match('/^\(.*\)$/', $part)) {
$replacement = str_replace(
array('AND', 'OR'), array('AND--', 'OR--'), $part
);
$where = str_replace($part, $replacement, $where);
}
}
$parts = preg_split(
'/(AND|OR)\s/', $where, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE
);
$type = 'where';
for ($i = 0 ; $i < count($parts) ; $i++) {
if ('OR' == $parts[$i]) {
$type = 'orWhere';
} else {
$type = 'where';
}
// $subQuery = str_replace('--', '', trim($parts[++$i],'() '));
$subQuery = preg_replace('/^\s*\((.+)\)\s*$/', '$1', $parts[++$i]);
$subQuery = str_replace('--', '', $subQuery);
$result .= "\r\t" . "->{$type}(\""
. $this->_replaceLongTableName(trim($subQuery)) . '")';
}
return $result;
}
It specifically screws up in the $replacement = preg_replace('/^\s*\((.+)\)\s*$/', '$1', $part); line…
To use IN parameter in where clause, try the following way
when the parameter for where is an array, Zend automatically splits by “,”