Skip to content

Commit 53ad5ec

Browse files
committed
GH-40 SQLBuilder should prepend table name to fields in apply_where_conditions if any joins have been specified
1 parent b782557 commit 53ad5ec

File tree

1 file changed

+24
-2
lines changed

1 file changed

+24
-2
lines changed

lib/SQLBuilder.php

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -271,14 +271,36 @@ public static function create_hash_from_underscored_string($name, &$values=array
271271
return $hash;
272272
}
273273

274+
/**
275+
* prepends table name to hash of field names to get around ambiguous fields when SQL builder
276+
* has joins
277+
*
278+
* @param array $hash
279+
* @return array $new
280+
*/
281+
private function prepend_table_name_to_fields($hash=array())
282+
{
283+
$new = array();
284+
$table = $this->connection->quote_name($this->table);
285+
286+
foreach ($hash as $key => $value)
287+
{
288+
$k = $this->connection->quote_name($key);
289+
$new[$table.'.'.$k] = $value;
290+
}
291+
292+
return $new;
293+
}
294+
274295
private function apply_where_conditions($args)
275296
{
276297
require_once 'Expressions.php';
277298
$num_args = count($args);
278299

279300
if ($num_args == 1 && is_hash($args[0]))
280301
{
281-
$e = new Expressions($this->connection,$args[0]);
302+
$hash = is_null($this->joins) ? $args[0] : $this->prepend_table_name_to_fields($args[0]);
303+
$e = new Expressions($this->connection,$hash);
282304
$this->where = $e->to_s();
283305
$this->where_values = array_flatten($e->values());
284306
}
@@ -323,7 +345,7 @@ private function build_insert()
323345
if ($this->sequence)
324346
{
325347
$sql =
326-
"INSERT INTO $this->table($keys," . $this->connection->quote_name($this->sequence[0]) .
348+
"INSERT INTO $this->table($keys," . $this->connection->quote_name($this->sequence[0]) .
327349
") VALUES(?," . $this->connection->next_sequence_value($this->sequence[1]) . ")";
328350
}
329351
else

0 commit comments

Comments
 (0)