Updates to ORM sub records and change Sort::MAsort, sortby is now an array

This commit is contained in:
Deon George 2015-09-29 15:51:56 +10:00
parent 9a41635025
commit 69c8052b53
2 changed files with 23 additions and 5 deletions

View File

@ -125,8 +125,11 @@ abstract class lnApp_ORM extends Kohana_ORM {
protected function _load_values(array $values) { protected function _load_values(array $values) {
parent::_load_values($values); parent::_load_values($values);
$sort = FALSE; if ($this->_sub_items_load AND count($this->_sub_items_load) > 1)
if ($this->_loaded AND $this->_sub_items_load AND count($this->_sub_items_load) == 1) throw HTTP_Exception::factory('501','Sub Items doesnt support more than 1 load');
$sort = array();
if ($this->_loaded AND $this->_sub_items_load)
foreach ($this->_sub_items_load as $item => $sort) foreach ($this->_sub_items_load as $item => $sort)
$this->_sub_items = $this->$item->find_all()->as_array(); $this->_sub_items = $this->$item->find_all()->as_array();
@ -337,6 +340,21 @@ 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) {
foreach ($this->_sub_items as $o)
if ($o->{$key} == $value)
return $o;
}
/**
* 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;
}
/** /**
* Override the Kohana processing so we can null values if required. * Override the Kohana processing so we can null values if required.
* We override this function, because we do set our own primary key value * We override this function, because we do set our own primary key value

View File

@ -19,9 +19,9 @@ abstract class lnApp_Sort {
* @param boolean Whether to reverse sort. * @param boolean Whether to reverse sort.
* @return array Sorted multi demension array. * @return array Sorted multi demension array.
*/ */
public static function MAsort(&$data,$sortby,$rev=0) { public static function MAsort(&$data,array $sortby=array(),$rev=0) {
// if the array to sort is null // if the array to sort is null
if (! $data) if (! $data OR ! $sortby)
return; return;
// if the array to sort is null or empty, or our sortby is bad // if the array to sort is null or empty, or our sortby is bad
# if (! preg_match('/^([a-zA-Z0-9_]+(\([a-zA-Z0-9_,]*\)(->[a-zA-Z0-9])?)?,?)+$/',$sortby) || ! $data) # if (! preg_match('/^([a-zA-Z0-9_]+(\([a-zA-Z0-9_,]*\)(->[a-zA-Z0-9])?)?,?)+$/',$sortby) || ! $data)
@ -29,7 +29,7 @@ abstract class lnApp_Sort {
$code = '$c=0;'; $code = '$c=0;';
foreach (explode(',',$sortby) as $key) { foreach ($sortby as $key) {
$code .= 'if (is_object($a) || is_object($b)) {'; $code .= 'if (is_object($a) || is_object($b)) {';
foreach (array('a','b') as $x) { foreach (array('a','b') as $x) {
$code .= 'if (is_array($'.$x.'->'.$key.')) {'; $code .= 'if (is_array($'.$x.'->'.$key.')) {';