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

发布于 2025-01-07 16:51:44 字数 910 浏览 6 评论 0原文

我试图在我的项目中使用蛋糕教程中的 Cake PHP Auth 部分。但这不起作用。

我创建了三个表“admin_users”,其中包含字段(id,用户名,密码,group_id,创建,修改),“admin_groups”(id,名称,创建,修改)和“用户”(id,用户名,密码,创建,修改) );

我尝试将普通用户放入用户表中,而管理员用户则放入“admin_users”表中,并按“admin_groups”对管理员用户进行分组,以便在管理端进行权限访问。

当我尝试路径“[rootpath]/users/login”时,出现错误:未找到模型管理员的数据库表“admins”。但我没有为管理员创建模型。

在我再次手动创建 admins 表后,出现错误“未找到模型 UsersAdmin 的数据库表“users_admin””。这里也没有为UsersAdmin创建模型。

在用户控制器中

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());
     }
}

I was trying to work with Cake PHP Auth Section in cake tutorials with my project. 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);

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.

When I try to path '[rootpath]/users/login' gives an error Database table 'admins' for model Admin was not found. 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". Here also dont created a model for 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());
     }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

酒废 2025-01-14 16:51:44

该问题可能与您的数据库结构有关。

首先介绍一下主题,但是如果您从头开始您的项目,您可能应该只使用一个表(users)来存储所有用户,并将它们链接到另一个表(groups) code>) 赋予他们不同的权利。它可以是与 users 表中的字段 group_id 的直接链接(这将允许您稍后使用核心 ACL 组件),也可以是与用户之间的表user_groups

也就是说,Cake 无法立即理解一些内容:您将 admin_usersadmin_groups 之间的外键称为“group_id” 。使用此名称,Cake 将尝试查找名为 groups 的表,而不是 admin_groups。如果您想保留admin_groups,则外键名称应为admin_group_id

首先纠正这个问题,也许这会解决您遇到的一些问题。如果您未处于调试模式,您可能还需要更正 AdminUser 模型以反映此更新并清理缓存。

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. 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.

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". With this name, Cake will try to find a table called groups, not admin_groups. If you want to keep with admin_groups, the foreign key name should be 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.

随梦而飞# 2025-01-14 16:51:44

尝试这样

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());
}

 }
 ?>

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());
}

 }
 ?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文