模型关联出错:Unknown column 'user_id' in 'where clause'

浏览:7877 发布日期:2016/10/10 分类:求助交流 关键字: 模型关联
我写了一个关联操作,其中控制器和模型都没有user_id字段,但是页面输出的时候提示Unknown column 'user_id' in 'where clause' 而且我也数据库也没有这个字段,很郁闷,是什么问题呢?

控制器<?php
namespace Home\Controller;
use Think\Controller;
class UserController extends Controller {
    public function index(){
        $user=D("user");        
        $arr=$user->field("id,username")->relation(true)->select();
        dump($arr);
        print_r($arr);exit;
    }
}
模型<?php
namespace Home\Model;
use Think\Model;
use Think\Model\RelationModel;
class UserModel extends RelationModel{
    Protected $_link=array(
        "card"=>array(
            "mapping_type"=>self::HAS_ONE,
        ),
    );
}
页面输出:
:(
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'user_id' in 'where clause'
错误位置
FILE: F:\website\thinkphp\ThinkPHP\Library\Think\Db\Driver.class.php  LINE: 156
TRACE
#0 F:\website\thinkphp\ThinkPHP\Library\Think\Db\Driver.class.php(156): PDO->prepare('SELECT * FROM `...')
#1 F:\website\thinkphp\ThinkPHP\Library\Think\Db\Driver.class.php(947): Think\Db\Driver->query('SELECT * FROM `...', false)
#2 F:\website\thinkphp\ThinkPHP\Library\Think\Model.class.php(759): Think\Db\Driver->select(Array)
#3 F:\website\thinkphp\ThinkPHP\Library\Think\Model\RelationModel.class.php(149): Think\Model->find()
#4 F:\website\thinkphp\ThinkPHP\Library\Think\Model\RelationModel.class.php(113): Think\Model\RelationModel->getRelation(Array, true)
#5 F:\website\thinkphp\ThinkPHP\Library\Think\Model\RelationModel.class.php(68): Think\Model\RelationModel->getRelations(Array, true)
#6 F:\website\thinkphp\ThinkPHP\Library\Think\Model.class.php(589): Think\Model\RelationModel->_after_select(Array, Array)
#7 F:\website\thinkphp\Demo\Home\Controller\UserController.class.php(7): Think\Model->select()
#8 [internal function]: Home\Controller\UserController->index()
#9 F:\website\thinkphp\ThinkPHP\Library\Think\App.class.php(173): ReflectionMethod->invoke(object(Home\Controller\UserController))
#10 F:\website\thinkphp\ThinkPHP\Library\Think\App.class.php(110): Think\App::invokeAction(object(Home\Controller\UserController), 'index')
#11 F:\website\thinkphp\ThinkPHP\Library\Think\App.class.php(204): Think\App::exec()
#12 F:\website\thinkphp\ThinkPHP\Library\Think\Think.class.php(120): Think\App::run()
#13 F:\website\thinkphp\ThinkPHP\ThinkPHP.php(97): Think\Think::start()
#14 F:\website\thinkphp\Demo\index.php(3): include('F:\website\thin...')
#15 {main}
ThinkPHP3.2.3 { Fast & Simple OOP PHP framework } -- [ WE CAN DO IT JUST THINK ]
最佳答案
评论( 相关
后面还有条评论,点击查看>>