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

Fatal error: primaryKey() #21

Open
tarzinio opened this issue Mar 28, 2022 · 11 comments
Open

Fatal error: primaryKey() #21

tarzinio opened this issue Mar 28, 2022 · 11 comments

Comments

@tarzinio
Copy link

Fatal error: Uncaught Error: Non-static method thecodeholic\phpmvc\db\DbModel::primaryKey() cannot be called statically in C:\wamp64\www\link\vendor\thecodeholic\php-mvc-core\Application.php on line 54

obraz

@thecodeholic
Copy link
Owner

Thanks for submitting the issue.
I will do my best to fix that ASAP. If you want you can make a PR I would merge it.
The changes should be made here.
https://github.com/thecodeholic/tc-php-mvc-core/blob/master/db/DbModel.php#L23

@HoosierExperienceDesigner
Copy link

HoosierExperienceDesigner commented Apr 1, 2022

I'm experiencing a similar issue but mine stems from the Application.php file. Everything was working perfect until about the 4:25:00 point in the lesson. I used var_dump at the bottom of Application constructor to target the specific bug, and for me it is occurring in this line:

$primaryKey = $this->userClass::primaryKey();

Fatal error: Uncaught Error: Non-static method app\models\User::primaryKey() cannot be called statically in D:\xampp\htdocs\I211_Project\core\Application.php:45 Stack trace: #0 D:\xampp\htdocs\I211_Project\public\index.php(24): app\core\Application->__construct('D:\xampp\htdocs...', Array) #1 {main} thrown in D:\xampp\htdocs\I211_Project\core\Application.php on line 45

var_dump indicates it is returning the id, and the PHP cookie does exist.

Screenshot 2022-04-01 163225

@RobertoVasquez01
Copy link

All these problems with static calls are due to the php version. It has to be higher than 7.4.
You can temporarily modify the function so that it is not static, but later in the lesson you will experience a similar problem. That's why it's better to update the php version

@tarzinio
Copy link
Author

tarzinio commented Apr 4, 2022 via email

@HoosierExperienceDesigner

I also applied PHP settings to 8.0.1, and to be sure ran 'php -v' in the terminal with the following result:

PS D:\xampp\htdocs\I211_Project> php -v
PHP 8.0.1 (cli) (built: Jan 5 2021 23:43:39) ( ZTS Visual C++ 2019 x64 )
Copyright (c) The PHP Group
Zend Engine v4.0.1, Copyright (c) Zend Technologies

I still get the same fatal error Non-static method app\models\User::primaryKey() cannot be called statically

@RobertoVasquez01
Copy link

Yes, I was wrong in the first part of my comment. It was because I'm using php version 7.3 and throughout the video tutorial I had to make a few small changes to take advantage of this great input from @thecodeholic, and I was quick to say that the error is based on the php version.
You have to MOMENTARY change the declaration: "abstract public static function primaryKey(): string;" from the DbModel class to this: "public function primaryKey(): string;" and call it like this: DbModel->primaryKey();
I repeat: This is momentary until the video tutorial continues, which then will have to return the abstract declaration... and it will no longer give you a problem.

@HoosierExperienceDesigner

Thank you for the information! Will give this a shot and provide feedback on whether or not it was successful.

@HoosierExperienceDesigner

I eventually started over and rebuilt the app from the beginning and got it to work.

@RobertoVasquez01
Copy link

Perfect!
Thanks to @thecodeholic

@rlhopkins
Copy link

Howdy, love this mvc-framework! Been trying to fix error around the end of Login section. I still keep getting error for "findOne".
I've read through these comments, get a different error, so I undid the changes.
This is the current error:
Fatal error: Uncaught Error: Non-static method app\core\DbModel::findOne() cannot be called statically in /var/www/html/familyweb/models/LoginForm.php:45 Stack trace: #0 /var/www/html/familyweb/controllers/AuthController.php(32): app\models\LoginForm->login() #1 [internal function]: app\controllers\AuthController->login() #2 /var/www/html/familyweb/core/Router.php(57): call_user_func() #3 /var/www/html/familyweb/core/Application.php(60): app\core\Router->resolve() #4 /var/www/html/familyweb/public/index.php(39): app\core\Application->run() #5 {main} thrown in /var/www/html/familyweb/models/LoginForm.php on line 45
login_error
login_error

@RobertoVasquez01
Copy link

Declare the findOne function as static in the DbModel class.
public static function findOne($where){
}

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