0

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

/**
 * 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();
}
4

2 回答 2

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
}

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

于 2012-12-24T20:23:36.273 回答
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() 之前运行。基本上弄清楚你在搜索什么——然后搜索它。将更容易回退相关错误和用户消息。还有我的菜鸟意见——当你到达 IF / Then 的第三个嵌套级别时——是时候考虑重构了。

于 2012-12-24T21:41:20.920 回答