maurobonfietti / slim4-api-skeleton

Useful skeleton for RESTful API development with PHP and Slim 4.
http://bit.ly/2nNNOZi
MIT License
132 stars 32 forks source link

Undefined property: stdClass:: #71

Closed mourafnd closed 2 years ago

mourafnd commented 2 years ago

Hi!

I tried the tutorial "Desarrollar una API RESTful con PHP y Slim 4" but a got a error when I use compose test after creating the player and teams endpoints.

There were 2 errors:                                                                                                                                                                          

1) Tests\integration\PlayerTest::testCreate                                                                                                                                                   
Undefined property: stdClass::$id                                                                                                                                                             

/var/www/site/tests/integration/PlayerTest.php:25                                                                                                                                             

2) Tests\integration\TeamTest::testCreate                                                                                                                                                     
Undefined property: stdClass::$id                                                                                                                                                             

/var/www/site/tests/integration/TeamTest.php:25

Any Ideas?

Thank so much!

maurobonfietti commented 2 years ago

Hi @mourafnd !  👋

How are the structure of your tables? Do you have an id auto increment?

These are my tables, for example:

-- PLAYER
CREATE TABLE `player` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

-- TEAM
CREATE TABLE `team` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `stadium_name` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `capacity` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

Let me know. Best Regards, Mauro.-

mourafnd commented 2 years ago

Hi! I was using the database.sql from slim4-soccer-api-demo https://github.com/maurobonfietti/slim4-soccer-api-demo. Now I tried with these new tables and I got the same error. I'll start again from the scratch and I'll let you know if I was successful.

mourafnd commented 2 years ago

I started again, deleted all the project but kept the database and now it works!!! I don't know what happened. Maybe I did something stupid and I don't remember LOL.

Thank you!

Update:

database.sql from https://github.com/maurobonfietti/slim4-soccer-api-demo:

CREATE TABLE `player` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

From your comment:

CREATE TABLE `player` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

AUTO_INCREMENT=1 is the only difference.

maurobonfietti commented 2 years ago

Excellent. I'm glad it worked for you now!

It's fine as it is in the repository.

At this moment, is required a field ID with an auto-increment, like this:

`id` int(11) NOT NULL AUTO_INCREMENT,

Thank you @mourafnd !