Database_Exception [1146]:表'database.roles'不存在[从`roles`显示全部列]

时间:2013-12-08 00:33:53

标签: php mysql kohana kohana-orm

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

控制器

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` ]

任何人都可以解释为什么它正在寻找一个名为角色的表吗?数据库中的表称为用户。

1 个答案:

答案 0 :(得分:2)

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

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

这是错误的起源(当你扩展所说的类时)。

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

模块/ ORM / AUTH-架构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;