Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect Error Message ? #1328

Closed
agus-prawoto opened this issue Oct 18, 2018 · 7 comments
Closed

Incorrect Error Message ? #1328

agus-prawoto opened this issue Oct 18, 2018 · 7 comments

Comments

@agus-prawoto
Copy link

Hi, when I call the session but it not initiated, CI4 gives an error on database instead of the session itself (this might confuse developers, they might guess the error come from database connection), for example, if I call:

$this->session->get('user_id');

form Models/WelcomeModel.php

it gives an error message:

CodeIgniter\Database\Exceptions\DatabaseException #8
You must set the database table to be used with your query. search →

BASEPATH/Database\BaseConnection.php at line 933

@puschie286
Copy link
Contributor

i guess the problem is the model - not the session
can you try it in the controller ? or something else without database dependency

@agus-prawoto
Copy link
Author

agus-prawoto commented Oct 19, 2018

Hi, in controller I got this error message:

ErrorException
Undefined property: App\Controllers\Welcome::$session

APPPATH/Controllers\Item.php at line 15

I think this error message is more meaningful rather than in the model

@lonnieezell
Copy link
Member

By default on controllers there is no class property with the session. You must define one yourself, or get an instance of the session use the Service or the helper:

session()->get('user_id');
   or 
$this->session = \Config\Services::session();
$this->session->get('user_id');

Not an error with the framework. You're just trying to use it like it's CI3...

@skywizzy
Copy link

agus

Bro you just saved my project, thanks bro, i have been battling with this same error thinking is from my database not knowing is session error, i got the misleading error message below while the main error is from session. thanks bro...

CRITICAL - 2023-07-11 11:25:36 --> You must set the database table to be used with your query.
in SYSTEMPATH\Database\BaseConnection.php on line 883.
1 SYSTEMPATH\Model.php(642): CodeIgniter\Database\BaseConnection->table(null)
2 SYSTEMPATH\Model.php(803): CodeIgniter\Model->builder()
3 APPPATH\Models\Superadmin_db.php(98): CodeIgniter\Model->__get('session')
4 APPPATH\Controllers\Superadmin_ci.php(37): App\Models\Superadmin_db->adminfetchTopMsg()
5 SYSTEMPATH\CodeIgniter.php(932): App\Controllers\Superadmin_ci->superadmin_dashboard()
6 SYSTEMPATH\CodeIgniter.php(497): CodeIgniter\CodeIgniter->runController(Object(App\Controllers\Superadmin_ci))
7 SYSTEMPATH\CodeIgniter.php(366): CodeIgniter\CodeIgniter->handleRequest(null, Object(Config\Cache), false)
8 FCPATH\index.php(67): CodeIgniter\CodeIgniter->run()

@kenjis
Copy link
Member

kenjis commented Jul 11, 2023

I recommend all of you don't use Session in Models.
Passing a value in the session to models is sufficient and better.

@skywizzy
Copy link

skywizzy commented Jul 11, 2023 via email

@agus-prawoto
Copy link
Author

agus-prawoto commented Jul 12, 2023

I totally agree that assign a value in the session to models is sufficient and better.
But in my opinion, sessions store global data, for example user data (after the user has successfully logged in), so if it is available globally it would be more beneficial, we can place it in BaseModel.php so we can access it from all models.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants