Home > Backend Development > PHP Tutorial > ThinkPHP 联系关系模型(二十)

ThinkPHP 联系关系模型(二十)

WBOY
Release: 2016-06-13 12:12:56
Original
1118 people have browsed it

ThinkPHP 关联模型(二十)

ThinkPHP关联模型


两表关联查询:Message和  user  关联条件uid(参考手册:模型->关联模型)


  步骤:
一:创建Message表自定义的Mode  --->Home\Lib\Model\MessageModel.class.php
class 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();
?>
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template