diff --git a/classes/lnApp/ORM.php b/classes/lnApp/ORM.php index d3ad9d1..db95e53 100644 --- a/classes/lnApp/ORM.php +++ b/classes/lnApp/ORM.php @@ -290,6 +290,20 @@ abstract class lnApp_ORM extends Kohana_ORM { return $result; } + /** + * Replace this ORM object with one in the database + */ + public function replace(array $key,$id='id') { + $o = ORM::factory(ucfirst($this->_object_name),$key); + + foreach ($this->changed() as $k) + $o->{$k} = $this->{$k}; + + $o->_sub_items = $this->_sub_items; + + return $o->save(); + } + /** * This function is used so that methods can be called via variables */ @@ -340,19 +354,17 @@ abstract class lnApp_ORM extends Kohana_ORM { array_push($this->_sub_items,$item); } - public function subitem_get($key,$value) { - foreach ($this->_sub_items as $o) - if ($o->{$key} == $value) - return $o; - } + public function subitem_get($model,array $key) { + $class = 'Model_'.$model; - /** - * Since DB records are stored as strings, this function enables us to convert - * values back to strings, primarily so they dont trigger being changed if they - * havent. - */ - public static function tostring($value) { - return (string)$value; + foreach ($this->_sub_items as $o) + if ($o instanceof $class AND array_intersect($o->object(),$key) === $key) + return $o; + + $item = ORM::factory($model); + array_push($this->_sub_items,$item); + + return $item; } /**