这是我的控制器
userdetails.php
<?php defined('SYSPATH') or die('No direct script access.');
class Controller_Userdetails extends Controller {
public function action_index() {
$view = new View('userdetails/index');
$this->response->body($view);
}
public function action_add() {
$userdetails = new Model_Userdetails();
$view = new View('userdetails/adddetails');
$view->set("userdetails", $userdetails);
$this->response->body($view);
}
型号为
userdetails.php
<?php defined('SYSPATH') or die('No direct script access.');
class Model_Userdetails extends ORM {
}
userinfo.sql
CREATE TABLE `kohana`.`userinfo` (
`id` int unsigned NOT NULL AUTO_INCREMENT,
`first_name` varchar(100) DEFAULT NULL,
`last_name` varchar(100) DEFAULT NULL,
`email` varchar(100) DEFAULT NULL,
`description` text,
PRIMARY KEY (`id`)
);
我刚开始学习php和kohana。我使用的是kohana 3.2版本。我遵循本教程来创建、编辑、更新和删除数据库中的数据。我尝试了上面的代码,我得到了这个错误
"Database_Exception [ 1146 ]: Table 'kohana.userdetailses' doesn't exist [ SHOW FULL COLUMNS FROM `userdetailses` ]"
需要一些帮助来解决这个问题。
Kohana在未设置$_table_name
的情况下猜测表名。它将模型名称复数化,因为大多数时候模型描述的是单个对象,而表中描述了很多对象。
编辑:
我看到您实际上将表命名为userinfo
,将模型命名为userdetails
。如果这是你想要的:
class Model_Userdetails {
protected $_table_name = 'userinfo';
........
}
或者将模型重命名为Model_Userinfo
,或将表重命名为userdetails
和:
---结束编辑---
在你的情况下,它似乎最适合:
class Model_Userdetails {
protected $_table_name = 'userdetails';
........
}
当然,您也可以将类重命名为Model_Userdetail
,这样表名就会被猜测为userdetails
更新:
尽管Kohana也应该猜测这些(即SHOW FULL COLUMNS FOR table
或sth),但这可能会解决property not found
错误,如下评论中所述。
protected $_table_columns = array(
'id' => array('type' => 'int'),
'firstname' => array('type' => 'string'),
....... // repeat for all columns
);
更新:
您的PHP代码中有一个拼写错误:deScription
。无论如何,定义表列是一个好习惯,因为每次首次初始化模型时,这将为您节省额外的查询。除了一些可以从数组中获取参数的验证内容(例如,TEXT
类型的description
或多或少没有长度限制,但您可能希望将其限制为2000个字符)