如果用户名存在,CodeIgniter按用户名获取userinfo,如果没有得到userID

What I'd like to create is basically something similar like Facebook does. If user have posted his/her's username, the link would be like: www.mysite.com/user/{username}, if not link would be like: www.mysite.com/user/{userid}. With the codes below I'm getting undefined indexes for all of the fields I'm trying to retrieve from the dataabase. It does work only with user_id but not with username. Any help would be much appreciated.

Link to profiles (Views):

<a href="<?=base_url()?>user/<?=isset($event['username']) ? $event['username'] : $event['creatoruserid']?>">
    <img src="<?=base_url()?>public/images/uploads/profiles/<?=$event['profilepic']?>" class="event-profile-pic">
</a>

Route to profiles:

$route['user/(:any)'] = "user/profile/$1";

User Controller with user method:

<?php

class User Extends CI_Controller
{

    public function __construct()
    {

        parent::__construct();
        $this->load->model('event_model');
        $this->load->model('user_model');
    }

    public function profile($user_id)
    {
        // All the data we need in profile page

        $data['user'] = $this->user_model->getUserInfo($user_id);
        $data['events'] = $this->event_model->getEventsById($user_id);
        $data['total_events_created'] = $this->user_model->TotalEventsCreated($user_id);
        $data['total_comments'] = $this->user_model->TotalComments($user_id);
        $data['total_attending_events'] = $this->user_model->TotalAttendingEvents($user_id);

        $this->load->view('/app/header');
        $this->load->view('/app/profile', $data);
        $this->load->view('/app/footer');

    }
}

Model for retrieving the userinfo from database:

<?php

class User_model extends CI_Model
{

    public function getUserInfo($user_id)
    {

        $this->db->select('*')->from('users')->where(['user_id' => $user_id]);

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

        return $query->first_row('array');
    }
}

You can probably handle this in your SQL layer. The proposition is to find the user record either by their username or by their user ID. So, in SQL (CodeIgniter Active Record) you can phrase this as:

$this->db->select('*')
         ->from('users')
         ->where(['user_id' => $user_id])
         ->or_where(['user_name'] => $user_id])
         ->limit(1);

In this case we are assuming the $user_id can be a string (user_name) or an integer (user_id). We are also assuming that you have no user_names that are numeric and may by chance match a user_id. To prevent multiple rows being returned, you could add a limit to the query.

Once you have a user record, then you will need to pass in the found user record's user_id in the other queries instead of the user_id passed into the function.

Please be sure your 'username' is match column name in DB, and can you add echo statements such as echo 'A'; echo 'B'; to determine exactly where is undefined indexes error is?