diff options
author | emkael <emkael@tlen.pl> | 2016-05-04 15:34:42 +0200 |
---|---|---|
committer | emkael <emkael@tlen.pl> | 2016-05-04 15:34:42 +0200 |
commit | 46afd2ec473ec4adc7e1de2a0d59872ba84288b1 (patch) | |
tree | 72b124da3bfe4a0cd00ec5c9df4cfcc2c9cce10a /app | |
parent | 3569ebb1e2aca9ec15c022b0e015c36ed7c8239c (diff) |
* ActiveRecord "magic" (find*By*, delete*By*) methods resolve COLUMN_MAPPING fields
Diffstat (limited to 'app')
-rw-r--r-- | app/php/db/ActiveRecord.php | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/app/php/db/ActiveRecord.php b/app/php/db/ActiveRecord.php index 49e862c..1176767 100644 --- a/app/php/db/ActiveRecord.php +++ b/app/php/db/ActiveRecord.php @@ -9,6 +9,41 @@ class ActiveRecord extends TActiveRecord { return $name; } + const DYNAMIC_METHODS = [ + 'findby', + 'findallby', + 'deleteby', + 'deleteallby' + ]; + + private function _getMappedMethodName($method) { + if (static::$COLUMN_MAPPING) { + $methodParts = []; + if (preg_match('/^(' . implode('|', self::DYNAMIC_METHODS) . ')(.*)$/i', $method, $methodParts)) { + $methodParameters = []; + $columnString = implode( + '|', + array_merge( + array_keys(static::$COLUMN_MAPPING), + array_values(static::$COLUMN_MAPPING) + ) + ); + $parameterRegex = '/(' . $columnString . ')(and|_and_|or|_or_)?/i'; + $method = $methodParts[1]; + if (preg_match_all($parameterRegex, $methodParts[2], $methodParameters, PREG_SET_ORDER)) { + foreach ($methodParameters as $parameter) { + $mappedColumn = array_search($parameter[1], static::$COLUMN_MAPPING); + $method .= ($mappedColumn !== FALSE) ? $mappedColumn : $parameter[1]; + if (count($parameter) > 2) { + $method .= $parameter[2]; + } + } + } + } + } + return $method; + } + public function __get($name) { $name = $this->_getMappedPropertyName($name); if (property_exists($this, $name)) { @@ -25,6 +60,10 @@ class ActiveRecord extends TActiveRecord { return parent::__set($name, $value); } + public function __call($method, $args) { + return parent::__call($this->_getMappedMethodName($method), $args); + } + } ?> |