I can't figure this one out; it's probably really obvious to everyone's eyes except mine.
Here is my update method for Kohana 3.
public function update($type, $id, $updates) {
$info = $this->getInfo($type);
$dbTable = $info['table'];
$updatesKeysToValues = array();
var_dump($updates);
foreach($updates as $key => $value) {
// if the value is null or the key isnt set for this, don't update!
if ($value === null OR ! isset($info['columnsToUpdateData'][$key])) continue;
$updatesKeyToValues[] = "`$key` = :$key";
}
$updatesKeyToValues = implode(', ', $updatesKeyToValues);
$query = 'UPDATE `' . $dbTable . '` SET ' . $updatesKeyToValues . ' WHERE id = :id LIMIT 1' ;
echo $query;
$dbQuery = DB::query(Database::UPDATE, $query);
foreach($updates as $key => $value) {
echo "$key === $value\n<br>";
$dbQuery->bind(':' . $key, $value);
}
$success = $dbQuery->bind(':id', $id)
->execute();
var_dump($success);
}
During every var_dump()
and echo
the data is fine. There is nothing to suggest why this is happening.
Essentially what I am doing is getting the data for this table from the config, building a query string with named params, looping and defining the named params and then executing. Instead of working, I end up with all fields the same (seems to be whatever was the last array value).
I can't seem to figure it out, can you? Many thanks for your time.
UPDATE
I just had a thought, are underscores valid in param names in a query?
ANOTHER UPDATE
Here is the output of the echo $query
UPDATE `personnel` SET `first_name` = :first_name, `last_name` = :last_name, `email` = :email WHERE id = :id LIMIT 1
I also cooked up that method of binding multiple params to the query too. I've never done it in a loop before, but I assumed it would work. In Kohana 2.x, I'd always used $bindings[] = '[email protected]'
etc, but the new Kohana doesn't accept an array as far as I can tell.
FINAL UPDATE
Thanks everyone, I think it is being passed by reference. I got around it by setting it to $updates[$key]
Looks like I could of also used the param()
method instead of bind. View source