gpt4 book ai didi

php - 数据库异常[1146]: Table 'database.roles' doesn't exist [ SHOW FULL COLUMNS FROM `roles` ]

转载 作者:行者123 更新时间:2023-11-29 08:16:47 28 4
gpt4 key购买 nike

我目前正在设置一个新项目并创建我的登录系统。由于某种原因,它似乎正在搜索角色表?到目前为止,这是我的结构:

Controller

public function action_index()
{
//Check login
if ( Auth::instance()->logged_in() ):
HTTP::redirect('/', 302);
endif;

if ( Request::current()->post() ):
if( Auth::instance()->login( $_POST['username'], $_POST['password'] ) ):
echo Debug::vars($_POST); exit;
endif;
endif;

$view = View::factory('index/home');

$index_page = $view->render();
$this->response->body($index_page);
}

型号

class Model_User extends Model_Auth_User {

protected $_table_name = "users";
protected $_primary_val = "user_id";

protected $_table_columns = array(
'user_id' => array('type' => 'int'),
'team_id' => array('type' => 'int'),
'username' => array('type' => 'string'),
'password' => array('type' => 'string'),
);

我得到的错误是

Database_Exception [ 1146 ]: Table 'database.roles' doesn't exist [ SHOW FULL COLUMNS FROM `roles` ]

谁能解释一下为什么它要寻找一个名为“角色”的表?数据库中的表称为用户。

最佳答案

正如您在 API browser 中看到的那样,Model_Auth_User 具有以下 has_many 关系:

"roles" => array(2) (
"model" => string(4) "Role"
"through" => string(11) "roles_users"
)

这是错误的根源(当您扩展所述类时)。

在 ORM 的模块文件夹中,您可以找到用于默认身份验证的 SQL 架构:

modules/ORM/auth-schema-mysql.sql

CREATE TABLE IF NOT EXISTS `roles` (
`id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT,
`name` varchar(32) NOT NULL,
`description` varchar(255) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `uniq_name` (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

INSERT INTO `roles` (`id`, `name`, `description`) VALUES(1, 'login', 'Login privileges, granted after account confirmation');
INSERT INTO `roles` (`id`, `name`, `description`) VALUES(2, 'admin', 'Administrative user, has access to everything.');

CREATE TABLE IF NOT EXISTS `roles_users` (
`user_id` int(10) UNSIGNED NOT NULL,
`role_id` int(10) UNSIGNED NOT NULL,
PRIMARY KEY (`user_id`,`role_id`),
KEY `fk_role_id` (`role_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE IF NOT EXISTS `users` (
`id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT,
`email` varchar(254) NOT NULL,
`username` varchar(32) NOT NULL DEFAULT '',
`password` varchar(64) NOT NULL,
`logins` int(10) UNSIGNED NOT NULL DEFAULT '0',
`last_login` int(10) UNSIGNED,
PRIMARY KEY (`id`),
UNIQUE KEY `uniq_username` (`username`),
UNIQUE KEY `uniq_email` (`email`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE IF NOT EXISTS `user_tokens` (
`id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT,
`user_id` int(11) UNSIGNED NOT NULL,
`user_agent` varchar(40) NOT NULL,
`token` varchar(40) NOT NULL,
`created` int(10) UNSIGNED NOT NULL,
`expires` int(10) UNSIGNED NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `uniq_token` (`token`),
KEY `fk_user_id` (`user_id`),
KEY `expires` (`expires`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

ALTER TABLE `roles_users`
ADD CONSTRAINT `roles_users_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE,
ADD CONSTRAINT `roles_users_ibfk_2` FOREIGN KEY (`role_id`) REFERENCES `roles` (`id`) ON DELETE CASCADE;

ALTER TABLE `user_tokens`
ADD CONSTRAINT `user_tokens_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE;

关于php - 数据库异常[1146]: Table 'database.roles' doesn't exist [ SHOW FULL COLUMNS FROM `roles` ],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20448447/

28 4 0