php-mvc-framework icon indicating copy to clipboard operation
php-mvc-framework copied to clipboard

Fatal error: primaryKey()

Open tarzinio opened this issue 3 years ago • 11 comments

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

tarzinio avatar Mar 28 '22 10:03 tarzinio

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

thecodeholic avatar Mar 29 '22 06:03 thecodeholic

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

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

RobertoVasquez01 avatar Apr 04 '22 04:04 RobertoVasquez01

I am using version 8.1

pon., 4 kwi 2022 o 06:56 RobertoVasquez01 @.***> napisał(a):

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

— Reply to this email directly, view it on GitHub https://github.com/thecodeholic/php-mvc-framework/issues/21#issuecomment-1087115363, or unsubscribe https://github.com/notifications/unsubscribe-auth/AG7LDYVQUKWUMGYJDNPBEVDVDJZALANCNFSM5R2XYSNA . You are receiving this because you authored the thread.Message ID: @.***>

tarzinio avatar Apr 04 '22 07:04 tarzinio

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

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.

RobertoVasquez01 avatar Apr 05 '22 22:04 RobertoVasquez01

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

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

Perfect! Thanks to @thecodeholic

RobertoVasquez01 avatar Apr 20 '22 02:04 RobertoVasquez01

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

rlhopkins avatar May 10 '22 17:05 rlhopkins

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

RobertoVasquez01 avatar May 11 '22 03:05 RobertoVasquez01