-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
Model->where method does not exist #4583
Comments
Can you show us the full stack trace of the exception thrown? |
And, have you tried changing the constructor to this? public function __construct($db, $validation)
{
parent::__construct($db, $validation);
helper('database');
} |
{
"title": "TypeError",
"type": "TypeError",
"code": 500,
"message": "method_exists(): Argument #1 ($object_or_class) must be of type object|string, null given",
"file": "/Users/caliv3r/Documents/Works/online-training/web_api/vendor/codeigniter4/framework/system/BaseModel.php",
"line": 1770,
"trace": [
{
"file": "/Users/caliv3r/Documents/Works/online-training/web_api/vendor/codeigniter4/framework/system/BaseModel.php",
"line": 1770,
"function": "method_exists",
"args": [
null,
"where"
]
},
{
"file": "/Users/caliv3r/Documents/Works/online-training/web_api/vendor/codeigniter4/framework/system/Model.php",
"line": 832,
"function": "__call",
"class": "CodeIgniter\\BaseModel",
"type": "->",
"args": [
"where",
[
{
"emp_code": "admin",
"password": "P@ssw0rd"
}
]
]
},
{
"file": "/Users/caliv3r/Documents/Works/online-training/web_api/app/Models/UserModel.php",
"line": 109,
"function": "__call",
"class": "CodeIgniter\\Model",
"type": "->",
"args": [
"where",
[
{
"emp_code": "admin",
"password": "P@ssw0rd"
}
]
]
},
{
"file": "/Users/caliv3r/Documents/Works/online-training/web_api/app/Controllers/Auth.php",
"line": 27,
"function": "findUserByEmpCodeAndPassword",
"class": "App\\Models\\UserModel",
"type": "->",
"args": [
"admin",
"P@ssw0rd"
]
},
{
"file": "/Users/caliv3r/Documents/Works/online-training/web_api/vendor/codeigniter4/framework/system/CodeIgniter.php",
"line": 936,
"function": "login",
"class": "App\\Controllers\\Auth",
"type": "->",
"args": []
},
{
"file": "/Users/caliv3r/Documents/Works/online-training/web_api/vendor/codeigniter4/framework/system/CodeIgniter.php",
"line": 432,
"function": "runController",
"class": "CodeIgniter\\CodeIgniter",
"type": "->",
"args": [
{}
]
},
{
"file": "/Users/caliv3r/Documents/Works/online-training/web_api/vendor/codeigniter4/framework/system/CodeIgniter.php",
"line": 333,
"function": "handleRequest",
"class": "CodeIgniter\\CodeIgniter",
"type": "->",
"args": [
null,
{
"handler": "file",
"backupHandler": "dummy",
"storePath": "/Users/caliv3r/Documents/Works/online-training/web_api/writable/cache/",
"cacheQueryString": false,
"prefix": "",
"file": {
"storePath": "/Users/caliv3r/Documents/Works/online-training/web_api/writable/cache/",
"mode": 416
},
"memcached": {
"host": "127.0.0.1",
"port": 11211,
"weight": 1,
"raw": false
},
"redis": {
"host": "127.0.0.1",
"password": null,
"port": 6379,
"timeout": 0,
"database": 0
},
"validHandlers": {
"dummy": "CodeIgniter\\Cache\\Handlers\\DummyHandler",
"file": "CodeIgniter\\Cache\\Handlers\\FileHandler",
"memcached": "CodeIgniter\\Cache\\Handlers\\MemcachedHandler",
"predis": "CodeIgniter\\Cache\\Handlers\\PredisHandler",
"redis": "CodeIgniter\\Cache\\Handlers\\RedisHandler",
"wincache": "CodeIgniter\\Cache\\Handlers\\WincacheHandler"
}
},
false
]
},
{
"file": "/Users/caliv3r/Documents/Works/online-training/web_api/public/index.php",
"line": 44,
"function": "run",
"class": "CodeIgniter\\CodeIgniter",
"type": "->",
"args": []
},
{
"file": "/Users/caliv3r/Documents/Works/online-training/web_api/vendor/codeigniter4/framework/system/Commands/Server/rewrite.php",
"line": 45,
"args": [
"/Users/caliv3r/Documents/Works/online-training/web_api/public/index.php"
],
"function": "require_once"
}
]
} |
No, I haven't tried yet. I'm calling UserModel in my controller. <?php
namespace App\Controllers;
use App\Controllers\BaseController;
use App\Models\UserModel;
use CodeIgniter\HTTP\ResponseInterface;
use Exception;
class Auth extends BaseController
{
public function login()
{
$rules = [
'username' => 'required',
'password' => 'required|min_length[0]|max_length[20]'
];
$input = $this->getRequestInput($this->request);
if (!$this->validateRequest($input, $rules)) {
return $this->getResponse($this->validator->getErrors(), ResponseInterface::HTTP_BAD_REQUEST);
}
$userModel = new UserModel();
try {
// $user = $userModel->where(['emp_code', $input['username'], $input['password']])->first();
$user = $userModel->findUserByEmpCodeAndPassword($input['username'], $input['password']);
} catch (Exception $e) {
return $this->getResponse(['401NF' => $e->getMessage()], ResponseInterface::HTTP_UNAUTHORIZED);
}
// ... rest of code
}
} Thanks for reply :) |
Yes, I forgot that. Change the parameters to nullable. public function __construct($db = null, $validation = null)
{
parent::__construct($db, $validation);
helper('database');
} |
Thank you so much! worked like a charm! Closing this issue now, thanks again for the help :) |
Hi, CI4 Team
I just ran into a problem when calling Model->where method
When ever I called $this->where, $this->asArray()->where, or (new UserModel)->where , I would encouter an error: 500 method does not exist.
Here is my model
I have read through CI documentation which says
This query may be modified by interjecting Query Builder commands as needed prior to calling this method:
But I can't use this method as the docs said.
What am I missing here? please kindly advice.
Thanks :)
The text was updated successfully, but these errors were encountered: