Search code examples
query-buildercodeigniter-4

Codeigniter 4 Undefined index: user_id


I am trying to show display a "join" result and only want to show the user_id, username, email only once for users table even if there are many records on the other table, so I tried to make a query builder like below:

Table users                          | Table add_game
                                     |
user_id | username | email           | game_id | user_id | ign    | acc_id

1       | userA    | [email protected] | 1       | 1       | ignA   | accA
2       | userB    | [email protected] | 2       | 1       | ignB   | accB
                                     | 1       | 2       | ignB   | accB
                                     | 3       | 2       | ignD   | accD 

Model :

<?php namespace App\Models;

use CodeIgniter\Database\ConnectionInterface;

class LoginModel{
    
    protected $db;

    public function __construct(ConnectionInterface &$db){
        $this->db =& $db;
    }


    public function login(string $str)
    {


        return $this->db->table('users')
                        
                        ->groupStart()
                            ->where('username', $str)
                            ->orWhere('email', $str)
                        ->groupEnd()
                        
                        ->join('add_game', 'add_game.user_id = users.user_id')
                        //->distinct('users.user_id')
                        //->select(("GROUP_CONCAT(game_id, ign, acc_id) AS userdata"))
                        ->get()
                        ->getResultArray();
    }
    
}

Controller :

public function login()
    {

        $data = [];
        helper(['form']);
        
        $validation =  \Config\Services::validation();

        $db = db_connect();
        $model = new LoginModel($db);
        $user = $model->login($this->request->getVar('userlogin'));

        $this->setUserSession($user);
        
            echo view('templates/header', $data);
            echo view('account/login', $data);
            echo view('templates/footer', $data);
        
        
    }


    private function setUserSession($user){
        $data = [
            'user_id' => $user['user_id'],
            'username' => $user['username'],
            'email' => $user['email'],
            'firstname' => $user['firstname'],
            'lastname' => $user['lastname'],
            'dob' => $user['dob'],
            'country' => $user['country'],
            'country_code' => $user['c_code'],
            'contact' => $user['contact'],
            'game_id' => $user['game_id'],
            'ign' => $user['ign'],
            'acc_id' => $user['acc_id'],
            'isLoggedIn' => true
        ];
        
        session()->set($data);
        return true;
    }

But right now I am getting

Undefined index: user_id

error message. Previously there was no issue or error when I was using without query builder for my login :

public function login(string $str, string $fields, array $data)
    {
        return $this->where('username', $data['userlogin'])->orWhere('email', $data['userlogin'])
                                  ->first();
    }

How to resolve this error?


Solution

  • As by your comment (image) your array looks like:

    Array
    (
      [0]=>Array
        (
          [user_id]=>1,
          [user_name]=>'test',
          //etc.
        )
    )
    

    You get the

    Undefined index: user_id

    error message, because of addressing wrongly the array while using 'user_id' => $user['user_id']

    the correct way is to add the index you want to retrieve like:

    $this->setUserSession($user[0]);   // where 0 can be changed to the index you pretend
    

    now the array is flattened and 'user_id' => $user['user_id'] doesn't throw an error anymore.