diff --git a/include/class.orm.php b/include/class.orm.php index 224e2ca1233f95ec95152bc98100f74cd3930b3d..0450ef42f7d9f137fc797c39660c10cb06e7e175 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) { }