Added replace(), subitem_get()

This commit is contained in:
Deon George 2015-04-22 10:58:47 +10:00 committed by Deon George
parent 69c8052b53
commit 668a929a1c

View File

@ -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;
}
/**