codeigniter api get call with param options for where mysql query php

pgx2nnw8  于 2021-06-23  发布在  Mysql
关注(0)|答案(1)|浏览(316)

我正在使用codeigniter来构建我的api,我正在尝试找到一种最好的方法来允许发送多个参数,然后运行我的modelwhere子句(如果它们存在的话)。我遇到了一些问题,如果有人能给我一些建议最好的做法额外我觉得我的整个设置只是越来越臃肿。
我的查询可以采用以下参数:

/v1/tags?status=1&parentId=1&order=desc&limit=10&offset=1

这是我的table。

id  int(11) NO  PRI     auto_increment            
parentId    int(11) NO              
name    varchar(250)    NO              
status  tinyint(4)  NO              
createdDate timestamp   NO      CURRENT_TIMESTAMP

这是我的控制器。

/**
 * READ TAGS
 */
public function tags_get() {

    // OPTIONALS:
    $parentId   = $this->get('parentId');
    $status     = $this->get('status');

    // DEFAULTS:
    $offset     = $this->get('offset');
    $order      = $this->get('order');
    $limit      = $this->get('limit');

    // WHERE QUERY:
    $where = [];

    // VALIDATE:
    if(isset($status)){

        if ($status != 'publish' && $status != 'future' && $status != 'draft' && $status != 'pending' && $status != 'private' && $status != 'trash') {

            $this->response(array(
                'status'  => FALSE,
                'message' => '(status) must be one of the following (publish|future|draft|pending|private|trash)'
            ), REST_Controller::HTTP_OK);

        }

        // ADD TO QUERY:
        $where['status'] = $status;

    }

    if(isset($parentId)){

        if (filter_var($parentId, FILTER_VALIDATE_INT) === false) {

            $this->response(array(
                'status'  => FALSE,
                'message' => '(parentId) must be int'
            ), REST_Controller::HTTP_BAD_REQUEST);

        }

        // ADD TO QUERY:
        $where['parentId'] = $parentId;

    }

    // IF NO PARAMS RETUNR ALL DATA
    $data = $this->user_model->get_tags($where, $order, $offset, $limit);
    if($data){

        $this->response([
            'status'  => TRUE,
            'message' => 'Success',
            'paging'  => $offset,
            'records' => count($data),
            'data'    => $data,
        ], REST_Controller::HTTP_OK);

    }else{

        $this->response([
            'status' => FALSE,
            'message' => 'Not found',
            'data' => []
        ], REST_Controller::HTTP_NOT_FOUND); 

    }

}

这是我的模型

function get_tags($where = [], $order = 'desc', $offset = 0, $limit = 100){

    // MAIN QUERY:
    $this->db->select('*');    
    $this->db->from('tags');

    // OPTIONAL WHERE QUERIES:
    foreach ($where as $key => $value) {
        $this->db->where($key, $value);
    }

    // DEFUALTS:
    $this->db->order_by('createdDate', $order); 
    $this->db->limit($limit, $offset);
    $query = $this->db->get();
    return ($query->num_rows() > 0) ? $query->result_array() : FALSE;

}

接受以下查询。

/v1/tags?status=0

这失败了,我应该在数据库中使用yes | no或on | off作为varchar而不是booleans吗?
更新:根据答案,我将改变状态,以接受以下值。

publish
future
draft
pending
private
trash

我也更新了我的控制器见上文。

6jygbczu

6jygbczu1#

老实说,你的方法很好,最好选择开关式,因为如果你想适应新的状态,数字可能会变得复杂,
举个例子,
出于某种原因,你的经理又给你的系统添加了3个属性,比如0,1,2,3,4
这意味着
0已关闭
1已打开
2正在挂起
3损坏
4被取消
将来你将无法根据他们的号码记住他们的状态,但如果你改用名字,你可以更好地理解。
最后,为了稳定起见,坚持使用已知的结构。

相关问题