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; 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 * 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); array_push($this->_sub_items,$item);
} }
public function subitem_get($key,$value) { public function subitem_get($model,array $key) {
foreach ($this->_sub_items as $o) $class = 'Model_'.$model;
if ($o->{$key} == $value)
return $o;
}
/** foreach ($this->_sub_items as $o)
* Since DB records are stored as strings, this function enables us to convert if ($o instanceof $class AND array_intersect($o->object(),$key) === $key)
* values back to strings, primarily so they dont trigger being changed if they return $o;
* havent.
*/ $item = ORM::factory($model);
public static function tostring($value) { array_push($this->_sub_items,$item);
return (string)$value;
return $item;
} }
/** /**