Issue
I'm following the cake tutorial for Cakephp 3.0
I'm in the part where I want to do the login page. I have a database called medical_wear
and a table user
.
When I enter my email and my password on the login page I get the following error :
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'medical_wear.users' doesn't exist Please try correcting the issue for the following table aliases: Users
I don't understand I don't have a Users table and I don't use a Users table in the code.
I have searched in many topics and in the documentation and I found nothing.
I have this in src/Controller/AppController.php
public function initialize() {
$this->loadComponent('Flash');
$this->loadComponent('Auth', [
'authenticate' => [
'Form' => [
'fields' => [
'username' => 'email',
'password' => 'password'
]
]
],
'loginAction' => [
'controller' => 'User',
'action' => 'login'
]
]);
// Autorise l'action display pour que notre controller de pages // continue de fonctionner.
$this->Auth->allow(['display']);
}
And in src/Controller/UsersController.php
public function login() {
if ($this->request->is('post')) {
$user = $this->Auth->identify();
if ($user) {
$this->Auth->setUser($user);
return $this->redirect($this->Auth->redirectUrl());
}
$this->Flash->error('Votre username ou mot de passe est incorrect.');
}
}
In src/Template/Users/login.ctp
<h1>Connexion</h1>
<?= $this->Form->create() ?>
<?= $this->Form->input('email') ?>
<?= $this->Form->input('password') ?>
<?= $this->Form->button('Login') ?>
<?= $this->Form->end() ?>
Solution
cakephp follow naming conventions
Database table and field naming convention
Table name must be in lowercase & plural. If there are more than one word in the table name then words should be separated by underscore.
Example : users, consultant_services, etc.
Field-Name should also be in lowercase and if more than one word then separated by underscore.
Example : id, consultant_service_name
ForeignKey name should be singular and tablename_id
Example : users_id or consultant_services_id
Model naming Convention
Model file names are upper camel case, more than one word should be separated by underscore.
Example : User.php or Consultant_service.php
Model class name are singular, usually this should be the database table name but in singular format.
Example : User or ConsultantService
So your code should be
class UserTable extends Table
{
public function initialize(array $config)
{
parent::initialize($config);
$this->table('user');
}
}
For more information conventions
Answered By - urfusion
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.