处理一组选择语句但条件不同的好方法是什么?

时间:2012-05-30 19:18:28

标签: php function codeigniter select

我面临的一个问题是,许多查询具有类似的select语句但是join / where语句不同。

以下是我通过CodeIgniter处理的代码示例。我通常做的是创建一个函数get(),它接受一组随机键/值。根据传递的键/值,它将生成并运行适当的查询。现在,我想知道这是一个很好的做事方式吗?因为正如您所看到的,此功能变得越来越复杂。最初,我有一些函数,比如get_all(),get_only_lessons()等等,但是不得不用一两行不同的代码重复相同的代码集。

我的问题是处理这个问题的最佳方法是什么。

function get($param = NULL)
{
    /*
    SELECT  m.id AS id, CAST(m.order_number AS SIGNED) AS order_number, m.name AS name, m.permalink as permalink, 
        m.suplesson_id as suplesson_id, CAST(sm.order_number AS SIGNED) AS suplesson_order_number
    FROM    lessons m
    JOIN    courses c ON m.course_id = c.id
    LEFT JOIN lessons sm ON m.suplesson_id = sm.id
    WHERE   [various]
    */

    $select =   'm.id AS id, CAST(m.order_number AS SIGNED) AS order_number, m.name AS name, m.permalink as permalink, ';
    $select .= ' m.suplesson_id as suplesson_id';

    if (isset($param['id']) || isset($param['suplesson_order_number']) || isset($param['permalink']))   
        $select .= ', CAST(sm.order_number AS SIGNED) AS suplesson_order_number ';

    $this->db->select($select);
    $this->db->from($this->table_name.' m');
    $this->db->join($this->courses_table_name.' c', 'm.course_id = c.id');

    if (isset($param['id']) || isset($param['suplesson_order_number']) || isset($param['permalink']))
        $this->db->join($this->table_name.' sm', 'm.suplesson_id = sm.id', 'left');

    // where clauses
    if (isset($param['course_id'])) 
        $this->db->where(array('c.id' => $param['course_id']));
    if (isset($param['id'])) 
        $this->db->where(array('m.id' => $param['id']));
    if (isset($param['order_number'])) 
        $this->db->where(array('m.order_number' => $param['order_number']));
    if (isset($param['permalink'])) 
        $this->db->like('m.permalink', $param['permalink'], 'none');
    if (isset($param['suplesson_id'])) 
        $this->db->where(array('m.suplesson_id' => $param['suplesson_id']));
    if (isset($param['suplesson_order_number'])) 
        $this->db->where(array('sm.order_number' => $param['suplesson_order_number']));
    if (isset($param['NULL'])) 
        $this->db->where('m.'.$param['NULL'].' IS NULL');
    if (isset($param['NOT NULL'])) 
        $this->db->where('m.'.$param['NOT NULL'].' IS NOT NULL');

    $this->db->order_by('order_number');

    // filter based on num_rows/offset
    if (isset($param['id']) || isset($param['permalink']))  
        $this->db->limit(1);
    if (isset($param['num_rows']) && isset($param['offset'])) 
        $this->db->limit($param['num_rows'], $param['offset']);

    $query = $this->db->get();

    // return row if expecting 1 result 
    if (isset($param['id']) || isset($param['suplesson_order_number']) || isset($param['permalink']))   
        return ($query->num_rows() == 1) ? $query->row_array() : NULL;

    return ($query->num_rows() > 0) ? $query->result_array() : NULL;
}

1 个答案:

答案 0 :(得分:0)

运行数据库查询的常用方法是将模型代码构造为具有多个函数调用,每个函数调用与一个SQL语句相关,例如:

function get_user($userId)
{
    $this->db->get_where('user', array('userId' => $userId))
    //...
    //...        
}

function delete_user($userId)
{
    $this->db->delete('user',array('userId' => $userId))
}

您可以创建一个名为User_model的模型类,其中包含读取/更新用户表所需的所有功能,因此在控制器中调用特定的模型函数

$user = $this->User_model->get_user($userId)

看起来您正在尝试构建一个巨型模型函数,该函数检查各种参数以确定要运行的SQL语句。这不是好设计,不适合Codeignitors MVC模型。 相反,为每个表创建一个单独的模型,然后在每个模型中为您希望运行的每个SQL操作创建单独的函数。从控制器调用这些模型以获取/更新/删除表中的数据。