<?php
use Think\Model;
use Org\Util\String;
class TestModel extends Model
{
protected $tableName = 'your_table';
/**
* 單條-條件查詢
*
* @param array $where 查詢條件
* @return \Think\mixed
*/
public function getOne($where)
{
return $this->where($where)->find();
}
/**
* 多條-條件查詢
*
* @param array $where 查詢條件
* @return \Think\mixed
*/
public function getAll($where)
{
return $this->where($where)->select();
}
/**
* 插入數據
*
* @param array $data
* @return \Think\mixed
*/
public function insertOne($data)
{
return $this->add($data);
}
/**
* 條件刪除
*
* @param array $where
* @return \Think\mixed
*/
public function deleteOne($where)
{
return $this->where($where)->delete();
}
/**
* 查詢字段最大值
*
* @param string $field
* @return \Think\mixed
*/
public function getMaxVal($field)
{
return $this->field("max(".$field.") as max")->find()['max'];
}
/**
* 條件更新
*
* @param array $where 條件
* @param array $data 數據
* @return Ambigous <boolean, unknown>
*/
public function updateAll($where,$data)
{
return $this->where($where)->save($data);
}
/**
* 分頁查詢
*
* @param array $where 條件
* @param string $order 排序字段
* @param number $limit 壹頁裏的數據條數
* @param number $page_index 頁碼
* @return array
*/
public function getByPage($where,$order,$limit,$page_index)
{
$result = M()->table('table_name')->where($where)->order($order)->limit($limit)->page($page_index)->select();
return $result;
}
/**
* 獲取頁數
*
* @param array $where 條件
* @param number $num 壹頁裏的數據
* @return number
*/
public function getPageNum($where,$num = 10)
{
$count = $this->where($where)->count();
return ceil($count/$num);
}
/**
* 條件查詢壹個字段
*
* @param array $where
* @param string $field
* @return \Think\mixed
*/
public function getFieldVal($where,$field)
{
return $this->where($where)->getField($field);
}
}