From 551a93b1c2a98fcc181ea1fd3b270f35d191c23e Mon Sep 17 00:00:00 2001 From: liugang Date: Fri, 1 Nov 2024 14:10:30 +0800 Subject: [PATCH] + [refac] convert the method findByXXX to where condition. --- lib/base/mao/mao.class.php | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/lib/base/mao/mao.class.php b/lib/base/mao/mao.class.php index c45ead3b8c..e10c02d5f9 100644 --- a/lib/base/mao/mao.class.php +++ b/lib/base/mao/mao.class.php @@ -648,8 +648,45 @@ class baseMao return $pairs; } + /** + * 把名为 findByXXX 的方法转换为 where 条件。 + * Convert the method findByXXX to where condition. + * + * @param string $method + * @param array $args + * @access private + * @return object the mao object. + */ + private function findBy(string $method, array $args) + { + $field = str_replace('findby', '', $method); + if(count($args) == 1) + { + $operator = 'eq'; + $value = $args[0]; + } + else + { + $operator = $args[0]; + $value = $args[1]; + } + + $this->setFields(['*']); + $this->conditions = [['field' => $field, 'operator' => $operator, 'value' => $value]]; + + return $this; + } + public function __call(string $method, array $args) { + $method = strtolower($method); + + /* + * 如果是findByxxx,转换为where条件语句。 + * findByxxx, xxx as will be in the where. + **/ + if(strpos($method, 'findby') !== false) return $this->findBy($method, $args); + if(method_exists($this->cache, $method)) return call_user_func_array([$this->cache, $method], $args); $this->app->triggerError("Method $method not found in class baseMao.", __FILE__, __LINE__, true);