1、api里如何传递页码和每页记录数 data :{category_id: '{$category.id}',page:++count+',10'},
page参数传递页码+数量,例如 page=‘1,10’ 取第一页 每页10条记录
api ListsController.php getCategoryPostLists()方法
$articles = $portalCategoryModel->paramsFilter($param, $findCategory->articles()->alias('post'))->select();
$findCategory = $portalCategoryModel->where('id', $categoryId)->find(); //获取当前分类MODEL,返回数据类型MODEL对象。
$findCategory->articles()->alias('post')
在portalCategoryModel.php里定义了articles方法
/**
* 关联文章表
* @return $this
*/
public function articles()
{
return $this->belongsToMany('PortalPostModel', 'portal_category_post', 'post_id', 'category_id'); //belongsToMany 多表关联,
}
alias 方法 设置表的别名。 为什么要设置别名呢,有啥作用??
paramsFilter方法 关联模型解析过滤参数,解析的参数:field 字段(select 。。。。from),ids (id in 。。。),id(id=。。),where 条件表达式( and 。。。。),page分页,limit(数量)。page参数存在时,limit参数无效。
这里只解析了页数,没有解析每页显示的数量,系统默认了20个,不知道怎么通过参数更改这个默认值。
/**
* @access public
* @param array $params 过滤参数
* @param model $model 关联模型
* @return model|array $this|链式查询条件数组
*/
public function paramsFilter($params, $model = null)

/** * @access public * @param array $params 过滤参数 * @param model $model 关联模型 * @return model|array $this|链式查询条件数组 */ public function paramsFilter($params, $model = null) { if (!empty($model)) { $_this = $model; } else { $_this = $this; } if (isset($_this->visible)) { //model 里设置的可暴露字段??? $whiteParams = $_this->visible; } // 设置field字段过滤 if (!empty($params['field'])) { $filterParams = $this->strToArr($params['field']); if (!empty($whiteParams)) { $mixedField = array_intersect($filterParams, $whiteParams); //比较两个数组的键值,并返回交集 ,指定的字段并且是可暴露的。 } else { $mixedField = $filterParams; } if (!empty($mixedField)) { $_this->field($mixedField); //model的field方法???? } } // 设置id,ids if (!empty($params['ids'])) { $ids = $this->strToArr($params['ids']); foreach ($ids as $key => $value) { $ids[$key] = intval($value); } } if (!empty($params['id'])) { $id = intval($params['id']); if (!empty($id)) { return $_this->where('id', $id); } } elseif (!empty($ids)) { $_this->where('id', 'in', $ids); } if (!empty($params['where'])) { if (empty($model)) { $_this->where($params['where']); } } // 设置分页 if (!empty($params['page'])) { $pageArr = $this->strToArr($params['page']); $page = []; foreach ($pageArr as $value) { $page[] = intval($value); } if (count($page) == 1) { $_this->page($page[0]); } elseif (count($page) == 2) { $_this->page($page[0], $page[1]); } } elseif (!empty($params['limit'])) { // 设置limit查询 $limitArr = $this->strToArr($params['limit']); $limit = []; foreach ($limitArr as $value) { $limit[] = intval($value); } if (count($limit) == 1) { $_this->limit($limit[0]); } elseif (count($limit) == 2) { $_this->limit($limit[0], $limit[1]); } } else { $_this->limit(10); } //设置排序 if (!empty($params['order'])) { $order = $this->strToArr($params['order']); foreach ($order as $key => $value) { $upDwn = substr($value, 0, 1); $orderType = $upDwn == '-' ? 'desc' : 'asc'; $orderField = substr($value, 1); if (!empty($whiteParams)) { if (in_array($orderField, $whiteParams)) { $orderWhere[$orderField] = $orderType; } } else { $orderWhere[$orderField] = $orderType; } } if (!empty($orderWhere)) { $_this->order($orderWhere); } } return $_this; }
// 设置分页
if (!empty($params['page'])) {
$pageArr = $this->strToArr($params['page']); //strToArr方法 用,分割字符串成数组。 $page = []; foreach ($pageArr as $value) { $page[] = intval($value); } if (count($page) == 1) { $_this->page($page[0]); } elseif (count($page) == 2) { $_this->page($page[0], $page[1]); //page给了两个参数,第一个是页码,第二个应该就是每页数量,think 的parseExpress方法里list($page, $listRows) = $options['page'];
$options['page']
} } elseif (!empty($params['limit'])) { // 设置limit查询 $limitArr = $this->strToArr($params['limit']); $limit = []; foreach ($limitArr as $value) { $limit[] = intval($value); } if (count($limit) == 1) { $_this->limit($limit[0]); } elseif (count($limit) == 2) { $_this->limit($limit[0], $limit[1]); } } else { $_this->limit(10); }
thinkphp 的think/db/query.php select 方法有一段,根据page参数解析limit
$options = $this->parseExpress(); //解析options
parseExpress方法如下代码
if (isset($options['page'])) {
// 根据页数计算limit
list($page, $listRows) = $options['page']; //list方法 把数组中的值复制给变量,参考: http://www.w3school.com.cn/php/func_array_list.asp
$page = $page > 0 ? $page : 1; //初始化page
$listRows = $listRows > 0 ? $listRows : (is_numeric($options['limit']) ? $options['limit'] : 20); // $options['page']参数里包含了每页的数量,没有就读取limit里指定的数量
$offset = $listRows * ($page - 1); //排除的记录数,
$options['limit'] = $offset . ',' . $listRows; // $listRows 默认20 第一页:0,20 第二页:20,20 从20条记录开始读取20条
}
}
parseExpress 方法:

/** * 分析表达式(可用于查询或者写入操作) * @access protected * @return array */ protected function parseExpress() { $options = $this->options; // 获取数据表 if (empty($options['table'])) { $options['table'] = $this->getTable(); } if (!isset($options['where'])) { $options['where'] = []; } elseif (isset($options['view'])) { // 视图查询条件处理 foreach (['AND', 'OR'] as $logic) { if (isset($options['where'][$logic])) { foreach ($options['where'][$logic] as $key => $val) { if (array_key_exists($key, $options['map'])) { $options['where'][$logic][$options['map'][$key]] = $val; unset($options['where'][$logic][$key]); } } } } if (isset($options['order'])) { // 视图查询排序处理 if (is_string($options['order'])) { $options['order'] = explode(',', $options['order']); } foreach ($options['order'] as $key => $val) { if (is_numeric($key)) { if (strpos($val, ' ')) { list($field, $sort) = explode(' ', $val); if (array_key_exists($field, $options['map'])) { $options['order'][$options['map'][$field]] = $sort; unset($options['order'][$key]); } } elseif (array_key_exists($val, $options['map'])) { $options['order'][$options['map'][$val]] = 'asc'; unset($options['order'][$key]); } } elseif (array_key_exists($key, $options['map'])) { $options['order'][$options['map'][$key]] = $val; unset($options['order'][$key]); } } } } if (!isset($options['field'])) { $options['field'] = '*'; } if (!isset($options['data'])) { $options['data'] = []; } if (!isset($options['strict'])) { $options['strict'] = $this->getConfig('fields_strict'); } foreach (['master', 'lock', 'fetch_pdo', 'fetch_sql', 'distinct'] as $name) { if (!isset($options[$name])) { $options[$name] = false; } } foreach (['join', 'union', 'group', 'having', 'limit', 'order', 'force', 'comment'] as $name) { if (!isset($options[$name])) { $options[$name] = ''; } } if (isset($options['page'])) { // 根据页数计算limit list($page, $listRows) = $options['page']; $page = $page > 0 ? $page : 1; $listRows = $listRows > 0 ? $listRows : (is_numeric($options['limit']) ? $options['limit'] : 20); $offset = $listRows * ($page - 1); $options['limit'] = $offset . ',' . $listRows; } $this->options = []; return $options; }