From dd5a57e0b75aee7ffe39da2ce216cc73f3f2981b Mon Sep 17 00:00:00 2001 From: Jared Hancock <jared@osticket.com> Date: Tue, 1 Apr 2014 14:21:48 -0500 Subject: [PATCH] orm: Add bulk update compilation --- include/class.orm.php | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/include/class.orm.php b/include/class.orm.php index 224e2ca12..0450ef42f 100644 --- a/include/class.orm.php +++ b/include/class.orm.php @@ -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) { } -- GitLab