Skip to content

Commit

Permalink
更多接口
Browse files Browse the repository at this point in the history
  • Loading branch information
dogstarTest committed Mar 11, 2020
1 parent f47e533 commit ce2c4ec
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 0 deletions.
50 changes: 50 additions & 0 deletions src/Model/DataModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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 二维数组
Expand All @@ -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);
}

}
33 changes: 33 additions & 0 deletions tests/src/Model/DataModel_Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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);
}

}

0 comments on commit ce2c4ec

Please sign in to comment.