美文网首页
[PHP高可用后端]③④--新闻搜索功能开发

[PHP高可用后端]③④--新闻搜索功能开发

作者: 子木同 | 来源:发表于2017-11-24 10:22 被阅读13次
微信截图_20171124095230.png

调试

News.php(Model)

public function getNewsByCondition($condition = [], $from, $size = 5)
    {
        if (!isset($condition['status'])) {
            $condition['status'] = [
                'neq', config('code.status_delete')
            ];
        }
        $order = ['id' => 'desc'];

        $result = $this->where($condition)
            ->field($this->getListField())
            ->limit($from, $size)
            ->order($order)
            ->select();
        //echo $this->getLastSql();exit();
        return $result;
    }

News.php(Controller)

<?php
/**
 * Created by PhpStorm.
 * User: tong
 * Date: 2017/11/23
 * Time: 17:03
 */

namespace app\api\controller\v1;

use app\api\controller\Common;

class News extends Common
{
    public function index()
    {
        //仿照之前讲解的validate验证机制 做相关检验
        $data = input('get.');

        $whereData['status'] = config('code.status_normal');
        if(!empty($data['catid'])){
            $whereData['catid'] = input('get.catid',0,'intval');
        }

        if (!empty($data['title'])) {
            $whereData['title'] = ['like', '%' . $data['title'] . '%'];
        }


        $this->getPageAndSize($data);
        $total = model('News')->getNewsByCountCondition($whereData);
        $news = model('News')->getNewsByCondition($whereData, $this->from, $this->size);

        $result = [
            'total' => $total,
            'page_num' => ceil($total / $this->size),
            'list' => $this->getDealNews($news),
        ];

        return show(1, 'OK', $result, 200);

    }
}
if(!empty($data['catid']
){
     $whereData['catid'] = input('get.catid',0,'intval');
}

if (!empty($data['title'])) {
     $whereData['title'] = ['like', '%' . $data['title'] . '%'];
}
) 微信截图_20171124102209.png

相关文章

网友评论

      本文标题:[PHP高可用后端]③④--新闻搜索功能开发

      本文链接:https://www.haomeiwen.com/subject/oottbxtx.html