Skip to content
Snippets Groups Projects
Commit dd5a57e0 authored by Jared Hancock's avatar Jared Hancock
Browse files

orm: Add bulk update compilation

parent edf157ce
No related branches found
No related tags found
No related merge requests found
......@@ -334,6 +334,19 @@ class QuerySet implements IteratorAggregate, ArrayAccess {
return $ex->affected_rows();
}
function update(array $what) {
$class = $this->compiler;
$compiler = new $class;
$ex = $compiler->compileBulkUpdate($this, $what);
$ex->execute();
return $ex->affected_rows();
}
function __clone() {
unset($this->_iterator);
unset($this->query);
}
// IteratorAggregate interface
function getIterator() {
$class = $this->iterator;
......@@ -416,6 +429,10 @@ class ModelInstanceIterator implements Iterator, ArrayAccess {
return $this->cache;
}
function objects() {
return clone $this->queryset;
}
// Iterator interface
function rewind() {
$this->position = 0;
......@@ -897,6 +914,19 @@ class MySqlCompiler extends SqlCompiler {
return new MysqlExecutor($sql, $this->params);
}
function compileBulkUpdate($queryset, array $what) {
$model = $queryset->model;
$table = $model::$meta['table'];
$set = array();
foreach ($what as $field=>$value)
$set[] = sprintf('%s = %s', $this->quote($field), $this->input($value));
$set = implode(', ', $set);
$where = $this->getWhereClause($queryset);
$joins = $this->getJoins();
$sql = 'UPDATE '.$this->quote($table).' SET '.$set.$joins.$where;
return new MysqlExecutor($sql, $this->params);
}
// Returns meta data about the table used to build queries
function inspectTable($table) {
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment