您好,欢迎访问一九零五行业门户网

框架 - thinkphp3.2 调用自定义Model类出现无法访问的一个问题?

使用thinkphp3.2版本
\home\controller目录下,新建testonecontroller继承了controller,其中有一个index方法。新建了一个testonemodel继承了model,设置一个test方法。两个类如下图:
之后访问testone模块的index方法,url:
http://localhost/weixin/index.php/home/testone/index
报错如下:
之后,尝试让testmodel不继承model后
再次访问
http://localhost/weixin/index.php/home/testone/index
访问成功
但是按照官方手册,不是应该需要继承吗?希望能给我解答下关于命名空间也好,继承也好,这是怎么一回事?
回复内容: 使用thinkphp3.2版本
\home\controller目录下,新建testonecontroller继承了controller,其中有一个index方法。新建了一个testonemodel继承了model,设置一个test方法。两个类如下图:
之后访问testone模块的index方法,url:
http://localhost/weixin/index.php/home/testone/index
报错如下:
之后,尝试让testmodel不继承model后
再次访问
http://localhost/weixin/index.php/home/testone/index
访问成功
但是按照官方手册,不是应该需要继承吗?希望能给我解答下关于命名空间也好,继承也好,这是怎么一回事?
我按照你的代码测试了一次发现没问题,是不是因为你前面用了d方法实例化模型留下缓存?
public function __construct($name='',$tableprefix='',$connection='') { // 模型初始化 $this->_initialize(); // 获取模型名称 if(!empty($name)) { if(strpos($name,'.')) { // 支持 数据库名.模型名的 定义 list($this->dbname,$this->name) = explode('.',$name); }else{ $this->name = $name; } }elseif(empty($this->name)){ $this->name = $this->getmodelname(); } // 设置表前缀 if(is_null($tableprefix)) {// 前缀为null表示没有前缀 $this->tableprefix = ''; }elseif('' != $tableprefix) { $this->tableprefix = $tableprefix; }elseif(!isset($this->tableprefix)){ $this->tableprefix = c('db_prefix'); } // 数据库初始化操作 // 获取数据库操作对象 // 当前模型有独立的数据库连接信息 $this->db(0,empty($this->connection)?$connection:$this->connection,true);}model 的构造方法 最后连接了数据库一般做法是一个 model 对应一个数据库表
经过测试你的代码没有任何的问题:

test(); }}?>
输出结果:
model类在实例化的时候默认会链接数据库,并且寻找和类名相同的表名,如果你在此之前没有添加对应的表会报错
如你的model为:testonemodel,数据库应该存在:xx_test_one表.
http://www.kancloud.cn/manual/thinkphp/1728
出现这个错误的原因是数据库未配置
config.php里配置
//数据库配置信息'db_type' => 'mysql', // 数据库类型'db_host' => '127.0.0.1', // 服务器地址'db_name' => 'thinkphp', // 数据库名'db_user' => 'root', // 用户名'db_pwd' => '123456', // 密码'db_port' => 3306, // 端口'db_params' => array(), // 数据库连接参数'db_prefix' => 'think_', // 数据库表前缀 'db_charset'=> 'utf8', // 字符集'db_debug' => true, // 数据库调试模式 开启后可以记录sql日志
并且数据库要有前缀_test_one表
其它类似信息

推荐信息