ThinkPHP 关联模型(二十) ThinkPHP关联模型 两表关联查询:Message和 user 关联条件uid(参考手册:模型->关联模型) 步骤:一:创建Message表自定义的Mode --->Home\Lib\Model\MessageModel.class.phpclass MessageModel extends RelationModel{//这是自动填充protected $_auto=array(array('time','time',1,'function'),array('uid','getId',1,'callback'));//这是关联模型配置protected $_link=array( 'User'=> array( 'mapping_type'=>BELONGS_TO,'class_name'=>'User','foreign_key'=>'uid', //外键'mapping_name'=>'user','mapping_fields'=>'username', //只取username字段'as_fields'=>'username:uname', //和当前表字段平级显示//要是怕2个表字段有冲突用username:uname 就会显示成uname), );protected function getId(){return $_SESSION['id'];}}?>二:控制器$message = D('Message');$arr=$message->relation(true)->select();dump($arr);$this->assagin('list',$arr);$this->display();?>