From ce2c4eca2ccc585ee28413b04368359a3106c1c5 Mon Sep 17 00:00:00 2001 From: dogstar Date: Wed, 11 Mar 2020 14:15:05 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E5=A4=9A=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Model/DataModel.php | 50 ++++++++++++++++++++++++++++++ tests/src/Model/DataModel_Test.php | 33 ++++++++++++++++++++ 2 files changed, 83 insertions(+) diff --git a/src/Model/DataModel.php b/src/Model/DataModel.php index 3e0a07d..eafb0d6 100644 --- a/src/Model/DataModel.php +++ b/src/Model/DataModel.php @@ -57,6 +57,26 @@ public function count($where = NULL, $countBy = '*') { return intval($total); } + /** + * 取最小值 + * @param string|array|NULL $where 统计条件 + * @param string $minBy 需要获取的字段 + * @return mixed + */ + public function min($where, $minBy) { + return $this->getORM()->where($where)->min($minBy); + } + + /** + * 取最大值 + * @param string|array|NULL $where 统计条件 + * @param string $maxBy 需要获取的字段 + * @return mixed + */ + public function max($where, $maxBy) { + return $this->getORM()->where($where)->max($maxBy); + } + /** * 求和 * @param string|array $where 查询条件,例如:id = 1,或数组形式array('id' => 1) @@ -248,6 +268,11 @@ public function updateCounter($where, array $updateData) { /** ---------------- 插入操作 ---------------- **/ + public function insert($data, $id = NULL) { + $id = parent::insert($data, $id); + return $id !== FALSE ? intval($id) : $id; + } + /** * 批量插入 * @param array $datas 二维数组 @@ -257,4 +282,29 @@ public function updateCounter($where, array $updateData) { public function insertMore($datas, $isIgnore = FALSE) { return $this->getORM()->insert_multi($datas, $isIgnore); } + + /** ---------------- SQL原生操作 ---------------- **/ + + /** + * 执行SQL查询语句,支持参数绑定 + * @param string $sql 完整的查询语句,例如:select * from user where id = :id,或:select * from user where id = ? + * @param array $parmas 需要动态绑定的参数,例如:array(':id' => 1),或:array(1) + * @return array 查询的结果集 + * @throws PDOException + */ + public function queryAll($sql, $parmas = array()) { + return $this->getORM()->queryAll($sql, $parmas); + } + + /** + * 执行SQL变更语句,支持参数绑定 + * @param string $sql 完整的变更语句 + * @param array $params 需要动态绑定的参数,例如:array(':id' => 1),或:array(1) + * @return int|boolean 返回影响的行数 + * @throws PDOException + */ + public function executeSql($sql, $params = array()) { + return $this->getORM()->executeSql($sql, $params); + } + } diff --git a/tests/src/Model/DataModel_Test.php b/tests/src/Model/DataModel_Test.php index 2eeb2b9..fc6a175 100644 --- a/tests/src/Model/DataModel_Test.php +++ b/tests/src/Model/DataModel_Test.php @@ -67,6 +67,24 @@ public function testCountByWhereMore() } + public function testMinByWhere() + { + $rs = $this->phalApiModelDataModel->min('id > 1 AND id < 10', 'id'); + + $this->assertTrue(is_int($rs)); + $this->assertGreaterThan(0, $rs); + + } + + public function testMaxByWhere() + { + $rs = $this->phalApiModelDataModel->max('id > 1 AND id < 10', 'id'); + + $this->assertTrue(is_int($rs)); + $this->assertGreaterThan(0, $rs); + + } + /** * @group testSum */ @@ -312,4 +330,19 @@ public function testNOtORM() { $this->assertInstanceOf('\NotORM_Result', $rs); } + public function testQueryAll() { + $sql = 'select * from tbl_demo where id < ?'; + $rs = $this->phalApiModelDataModel->queryAll($sql, array(10)); + // var_dump($rs); + $this->assertNotEmpty($rs); + } + + public function testExecuteSql() { + $sql = 'update tbl_demo set age = age + 1 where id = :id'; + $params = array(':id' => 776); + $rs = $this->phalApiModelDataModel->executeSql($sql, $params); + // var_dump($rs); + $this->assertEquals(0, $rs); + } + }