返回特定行或所有行的结果

时间:2012-12-24 19:43:44

标签: php codeigniter

我开发了这个功能,以帮助我快速从数据库中获取有关我的用户的信息。我的问题是,当我只需要一个用户时,或者当我需要多个用户时,只返回一行,这将是最好的回报。

/**
 * get_users function.
 * 
 * @access public
 * @param array $params (default: array();) 
 * possible array keys:  
 * user_ids - either an array of user ids or a single user id
 * user_status_ids - either an array of user status ids or a single user status id
 * user_role_ids - either an array of user role ids or a single user role id
 * 
 * @return object/NULL
 * Should return user_id, username, CONCAT(users.first_name, users.last_name, email_address, lock_date, user_status_name, user_role_name
 */
public function get_users($params = array())
{
    $this->db->select('users.user_id');
    $this->db->select('users.username');
    $this->db->select('CONCAT(users.first_name, " ", users.last_name) AS full_name', FALSE);
    $this->db->select('users.password');
    $this->db->select('users.password_hash');
    $this->db->select('users.email_address');
    $this->db->select('users.lock_date');
    $this->db->select('users.user_status_id');
    $this->db->select('users.user_role_id');
    $this->db->select('user_statuses.user_status_name');
    $this->db->select('user_roles.user_role_name');

    $this->db->from('users');
    $this->db->join('user_statuses', 'user_statuses.user_status_id = users.user_status_id');
    $this->db->join('user_roles', 'user_roles.user_role_id = users.user_role_id');

    //checking to see if any $params are attempting to be passed
    if (count($params) > 0)
    {
        //start title specific selection
        if (isset($params['user_ids']))
        {
            //if you only have one integer.
            if (is_numeric($params['user_ids']))
            {
                $this->db->where('users.user_id', $params['user_ids']);
            }
            else
            {
                if (is_array($params['user_ids']))
                {
                    $a = 0;
                    foreach($params['user_ids'] as $user_id)
                    {
                        if ($a == 0)
                        {
                            $this->db->where('users.user_id', $user_id);
                        }
                        else
                        {
                            $this->db->or_where('users.user_id', $user_id);
                        }
                        $a++;
                    }
                }
            }
        }

        //start title specific selection
        if (isset($params['usernames']))
        {
            //if you only have one integer.
            if (is_string($params['usernames']))
            {
                $this->db->where('users.username', $params['usernames']);
            }
            else
            {
                if (is_array($params['usernames']))
                {
                    $a = 0;
                    foreach($params['usernames'] as $username)
                    {
                        if ($a == 0)
                        {
                            $this->db->where('users.usernames', $username);
                        }
                        else
                        {
                            $this->db->or_where('users.username', $username);
                        }
                        $a++;
                    }
                }
            }
        }

        //start title specific selection
        if (isset($params['user_status_ids']))
        {
            //if you only have one integer.
            if (is_numeric($params['user_status_ids']))
            {
                $this->db->where('users.user_status_id', $params['user_status_ids']);
            }
            else
            {
                if (is_array($params['user_status_ids']))
                {
                    $a = 0;
                    foreach($params['user_status_ids'] as $user_status_id)
                    {
                        if ($a == 0)
                        {
                            $this->db->where('users.user_status_id', $user_status_id);
                        }
                        else
                        {
                            $this->db->or_where('users.user_status_id', $user_status_id);
                        }
                        $a++;
                    }
                }
            }
        }

        //start title specific selection
        if (isset($params['user_role_ids']))
        {
            //if you only have one integer.
            if (is_numeric($params['user_role_ids']))
            {
                $this->db->where('users.user_role_id', $params['user_role_ids']);
            }
            else
            {
                if (is_array($params['user_role_ids']))
                {
                    $a = 0;
                    foreach($params['user_role_ids'] as $user_role_id)
                    {
                        if ($a == 0)
                        {
                            $this->db->where('users.user_role_id', $user_role_id);
                        }
                        else
                        {
                            $this->db->or_where('users.user_role_id', $user_role_id);
                        }
                        $a++;
                    }
                }
            }
        }           
    }

    $query = $this->db->get();
    return $query->result();
}

2 个答案:

答案 0 :(得分:1)

最好将您的功能分成几个功能,这些功能可以很容易地理解查询。 使用一个参数创建一个函数,例如:

/**
 * Return user that match given ids
 * @param array $uids the list of users to find
 * @return array - the users that matched the list
 * @throws DatabaseException on any error
 */
public function listUserById( array $uids )
{

}

/**
 * Return user that match given id and status
 * @param array $uids the list of users to find
 * @param array $status the list of user status to also find
 * @return array - the users that matched the list
 * @throws DatabaseException on any error
 */
public function listUserByIdAndStatus( array $uids, array $status )
{
  ///code
}

或者您可以更新文档以指示您的函数返回“列表”(始终为数组)。

答案 1 :(得分:1)

所以我建议你看一下你方法的这一部分

 //checking to see if any $params are attempting to be passed
if (count($params) > 0)
{
    //start title specific selection
    if (isset($params['user_ids']))

 ETC ETC 

将所有这些放在一个单独的方法中,该方法在调用get_users()之前运行。基本上找出你要搜索的内容 - 然后搜索它。将更容易回放相关的错误和用户消息。我的noob意见 - 当你到达IF / Then的第三个嵌套级别时 - 是时候考虑重构了。