PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, February 21, 2022

[FIXED] Cakephp login page ask for table users but I only have a user table

 February 21, 2022     cakephp, cakephp-3.0, login, mysql, php     No comments   

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

  1. 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.
    
  2. Field-Name should also be in lowercase and if more than one word then separated by underscore.

    Example : id, consultant_service_name
    
  3. ForeignKey name should be singular and tablename_id

    Example : users_id or consultant_services_id
    

Model naming Convention

  1. Model file names are upper camel case, more than one word should be separated by underscore.

    Example : User.php or Consultant_service.php
    
  2. 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
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing