From 69c8052b53a2686e2a6893611a68cdcdde6c8659 Mon Sep 17 00:00:00 2001 From: Deon George Date: Tue, 29 Sep 2015 15:51:56 +1000 Subject: [PATCH] Updates to ORM sub records and change Sort::MAsort, sortby is now an array --- classes/lnApp/ORM.php | 22 ++++++++++++++++++++-- classes/lnApp/Sort.php | 6 +++--- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/classes/lnApp/ORM.php b/classes/lnApp/ORM.php index ee008f7..d3ad9d1 100644 --- a/classes/lnApp/ORM.php +++ b/classes/lnApp/ORM.php @@ -125,8 +125,11 @@ abstract class lnApp_ORM extends Kohana_ORM { protected function _load_values(array $values) { parent::_load_values($values); - $sort = FALSE; - if ($this->_loaded AND $this->_sub_items_load AND count($this->_sub_items_load) == 1) + if ($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) $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); } + 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. * We override this function, because we do set our own primary key value diff --git a/classes/lnApp/Sort.php b/classes/lnApp/Sort.php index 365a483..07b17be 100644 --- a/classes/lnApp/Sort.php +++ b/classes/lnApp/Sort.php @@ -19,9 +19,9 @@ abstract class lnApp_Sort { * @param boolean Whether to reverse sort. * @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 (! $data) + if (! $data OR ! $sortby) return; // 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) @@ -29,7 +29,7 @@ abstract class lnApp_Sort { $code = '$c=0;'; - foreach (explode(',',$sortby) as $key) { + foreach ($sortby as $key) { $code .= 'if (is_object($a) || is_object($b)) {'; foreach (array('a','b') as $x) { $code .= 'if (is_array($'.$x.'->'.$key.')) {';