Skip to content
Advertisement

Why does the auth component access the `users` table, despite me not using a model named `Users`?

I’m using cakephp 4 and when I’m trying to login a User it says me there was no table called Users. But I used “Users” nowhere in my code, only “User” without the “s”.

CakePHP error screen

I don’t find the bug and how to resolve it.

Advertisement

Answer

This is what can easily happen when you’re fighting the naming conventions, as the CakePHP framework follows a convention over configuration approach.

The conventions for database table names (and table classes for that matter) is to use plurals, so users instead of user, hence wherever CakePHP provides model/table names as defaults, it will use plural names accordingly.

By default the auth component’s authenticators (which is deprecated btw, you should look into using the authentication plugin instead) are configured to use the Users model/table alias, plural. The related option is userModel, which you would need to change accordingly if you wanted to use a different model. There’s other options that use plurals too, like the ones that define URL arrays, they use Users to target UsersController (controller names are by convention plural too).

The model layer also has entities, and the convention for entities is to use singular names, so User. This is another reason not to use singular names for tables, even if you can by configuring things, it can easily cause confusion with entity class names.

I would very strongly suggest that you follow the conventions, and that you use use the bake console to create your models, controllers, etc., especially if you’re a beginner. Once you’re familiar with the ins and outs of CakePHP you can start with bending it to your will, and deviate from the conventions in case necessary.

See also

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement