简体   繁体   English

Cake PHP Auth错误缺少数据库表。

[英]Cake PHP Auth Error missing database table .

I was trying to work with Cake PHP Auth Section in cake tutorials with my project. 我试图在我的项目的蛋糕教程中使用Cake PHP Auth Section。 But it doesn't work. 但这是行不通的。

I have created three tables 'admin_users' with fields(id,username,password,group_id,created,modified) , 'admin_groups'(id,name,created, modified) and 'users'(id,username,password,created,modified); 我已经创建了三个表“ admin_users”,其中包含字段(id,用户名,密码,group_id,创建,修改),“ admin_groups”(id,名称,创建,修改)和“ users”(id,用户名,密码,创建,修改) );

I tried normal users will be put in users table and admin users are in 'admin_users' table and grouped the admin users by 'admin_groups' for permission acess in admin side. 我试过将普通用户放在用户表中,将管理员用户放在“ admin_users”表中,并按“ admin_groups”将admin用户分组,以获取管理员方面的权限。

When I try to path '[rootpath]/users/login' gives an error Database table 'admins' for model Admin was not found. 当我尝试路径'[rootpath] / users / login'给出错误时,找不到模型Admin的数据库表'admins'。 But I dont created a model for admin. 但是我没有为管理员创建模型。

After I maually created the admins table again error "Database table 'users_admin' for model UsersAdmin was not found". 在再次正式创建admins表后,错误“找不到模型UsersAdmin的数据库表'users_admin'”。 Here also dont created a model for UsersAdmin. 这里也没有为UsersAdmin创建模型。

In Users Controller 在用户控制器中

class UsersController extends AppController {

    var $name = 'Users';
    var $uses = array();
    var $components = array('Auth','Session');


    function login() {
         $this->set('title_for_layout', 'Login');
         /*if ($this->Auth->user()) {
             $this->redirect($this->Auth->redirect());
         }*/
     }

     function logout() {
         $this->redirect($this->Auth->logout());
     }
}

The problem is probably related to your database structure. 该问题可能与您的数据库结构有关。

A bit of topic first, but if you start your project from scratch, you should probably use only one table ( users ) to store all users, and link them to another table ( groups ) to give them different rights. 首先要介绍一些话题,但是如果您从头开始项目,则可能应该只使用一个表( users )来存储所有用户,并将它们链接到另一个表( groups )以赋予他们不同的权限。 It could be a direct link with a field group_id in the users table (which will allow you later to use the core ACL component), or a many to many relation with a table users_groups between users and groups . 它可以是与users表中的字段group_id的直接链接(以后将允许您使用核心ACL组件),或者是usersgroups之间与表users_groups多对多关系。

That said, there is something that Cake can't understand out of the box: you called the foreign key between admin_users and admin_groups " group_id ". 也就是说,Cake开箱即admin_users无法理解某些内容:您将admin_usersadmin_groups之间的外键称为“ group_id ”。 With this name, Cake will try to find a table called groups , not admin_groups . 使用此名称,Cake将尝试查找名为groups而不是admin_groups的表。 If you want to keep with admin_groups , the foreign key name should be admin_group_id . 如果要保留admin_groups ,则外键名称应为admin_group_id

Correct this first, and maybe this will resolve some of the problems you get. 首先更正此问题,也许可以解决您遇到的一些问题。 You may also will have to correct the AdminUser model to reflect this update and clean the cache if you are not in debug mode. 如果您未处于调试模式,则可能还必须更正AdminUser模型以反映此更新并清理缓存。

Try like this 这样尝试

var $name = 'Users';
var $layout = 'main';

function beforeFilter(){

   $this->Auth->authenticate = ClassRegistry::init('User');
    parent::beforeFilter(); 
}

function index(){

    $this->set('title_for_layout', 'test site');
}


function login(){   

    Security::setHash('md5');       
    $this->set('title_for_layout', 'test site');

}


function welcome(){

}


function logout(){

    $this->set('title_for_layout', 'test site');

    $this->redirect($this->Auth->logout());
}

 }
 ?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM