From 40e1ac2e832155872204c51479f2d100a38c0478 Mon Sep 17 00:00:00 2001 From: Jared Hancock <jared@osticket.com> Date: Tue, 1 Apr 2014 15:27:48 -0500 Subject: [PATCH] orm: Add ::count() and ::expunge() to InstrumentedList --- include/class.orm.php | 36 ++++++++++++++++++++++++++---------- 1 file changed, 26 insertions(+), 10 deletions(-) diff --git a/include/class.orm.php b/include/class.orm.php index 46a6d6b83..16bdad711 100644 --- a/include/class.orm.php +++ b/include/class.orm.php @@ -435,10 +435,6 @@ class ModelInstanceIterator implements Iterator, ArrayAccess { return $this->cache; } - function objects() { - return clone $this->queryset; - } - // Iterator interface function rewind() { $this->position = 0; @@ -507,23 +503,43 @@ class InstrumentedList extends ModelInstanceIterator { $this->resource = null; } - function add($object) { + function add($object, $at=false) { if (!$object || !$object instanceof $this->model) throw new Exception('Attempting to add invalid object to list'); - $object->{$this->key} = $this->id; + $object->set($this->key, $this->id); $object->save(); - $this->list[] = $object; + + if ($at !== false) + $this->cache[$at] = $object; + else + $this->cache[] = $object; } function remove($object) { $object->delete(); } + function reset() { + $this->cache = array(); + } + + // QuerySet delegates + function count() { + return $this->queryset->count(); + } + function exists() { + return $this->queryset->exists(); + } + function expunge() { + return $this->queryset->delete(); + } function update(array $what) { return $this->queryset->update($what); } - function reset() { - $this->cache = array(); + + // Fetch a new QuerySet + function objects() { + return clone $this->queryset; } function offsetUnset($a) { @@ -533,7 +549,7 @@ class InstrumentedList extends ModelInstanceIterator { function offsetSet($a, $b) { $this->fillTo($a); $this->cache[$a]->delete(); - $this->add($b); + $this->add($b, $a); } } -- GitLab