ThinkPHP 关联模型(二十)
ThinkPHP关联模型
两表关联查询:Message
和 user 关联条件uid(参考手册:模型->关联模型)
步骤:
一:创建Message表自定义的Mode --->Home\Lib\Model\MessageModel.class.php
<?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'];
}
}
?>
二:控制器
<?php
$message = D('Message');
$arr=$message->relation(true)->select();
dump($arr);
$this->assagin('list',$arr);
$this->display();
?>
ThinkPHP 关联模型(二十)的更多相关文章
- ThinkPHP关联模型详解
在ThinkPHP中,关联模型更类似一种mysql中的外键约束,但是外键约束更加安全,缺点却是在写sql语句的时候不方便,ThinkPHP很好得解决了这个问题.但是很多人不动关联模型的意思.现在就写个 ...
- ThinkPHP关联模型如何关联非主键
ThinkPHP关联模型默认是主键外键关联 官方并没有提供相关文档 如何实现非主键与非主键间之间的关联 <?php namespace Admin\Model; use Think\Model\ ...
- thinkphp 关联模型配置代码
<?php /** * 公司与部门关联模型 */ class CompanyRelationModel extends RelationModel{ //主表名称 protected $tabl ...
- ThinkPHP 关联模型中查询某条记录的父级(非查询子级)
数据表 id cat_name cat_pid 76 手机.数码 0 84 手机配件 76 86 蓝牙耳机 84 从属关 ...
- ThinkPHP - 关联模型 - 多对多
表结构: 映射关系: UserRelationModel会取UserRelation为表名称.所以要自定义表名称: //定义主表名称protected $tableName = 'User'; < ...
- Thinkphp 关联模型和试图模型区别
关联模型主要在多表操作时使用,比如 user表,user_role表,role表 user_role字段:uid,rid,它作为中间表,负责将user和role之间的,1对1,1对多,多对多的关系进行 ...
- Thinkphp 关联模型
1.定义关联模型 2.使用关联模型 D('BlogRelation')->relation(true)->add($data);
- Thinkphp关联模型使用
1.需求描述 首页文章列表,需要同时获取文章的点赞和被关注数,同时如果被当前用户点赞或关注了会显示相应小图标进行区别.图示如下: 2.解决方案 数据库设计: 文章对应Article表,其中包括收藏数字 ...
- ThinkPHP - 关联模型 - 一对多
使用之前,先引入文件夹,否则相应的功能不能实现. 如果对thinkPHP不精通,使用或开发的时候,最好直接使用完成版本的ThinkPHP. 关系模型定义: <?php /** * 继承自 Rel ...
随机推荐
- java list三种遍历方法性能比较
从c/c++语言转向java开发,学习java语言list遍历的三种方法,顺便测试各种遍历方法的性能,测试方法为在ArrayList中插入1千万条记录,然后遍历ArrayList,发现了一个奇怪的现象 ...
- 正则表达式引擎的构建——基于编译原理DFA(龙书第三章)——3 计算4个函数
整个引擎代码在github上,地址为:https://github.com/sun2043430/RegularExpression_Engine.git nullable, firstpos, la ...
- NVelocity 实例
using System; using System.IO; using System.Collections; using System.Collections.Generic; using Sys ...
- WCF技术剖析之十九:深度剖析消息编码(Encoding)实现(下篇)
原文:WCF技术剖析之十九:深度剖析消息编码(Encoding)实现(下篇) [爱心链接:拯救一个25岁身患急性白血病的女孩[内有苏州电视台经济频道<天天山海经>为此录制的节目视频(苏州话 ...
- SLF4J warning or error messages and their meanings(转)
The method o.a.commons.logging.impl.SLF4FLogFactory#release was invoked. Given the structure of the ...
- 函数指针&绑定: boost::functoin/std::function/bind
see link: https://isocpp.org/wiki/faq/pointers-to-members function vs template: http://stackoverflow ...
- C++断言assert
assert宏是在标准库中提供的.它在库文件<cassert>中声明,它能够在程序中測试逻辑表达式,假设指定的逻辑表达式是false,assert()就会终止程序,并显示诊断消息.关闭断言 ...
- android Asynctask的优缺点?是否能同一时候并发100+asynctask呢?
一 Asynctask的优缺点? AsyncTask,是android提供的轻量级的异步类,能够直接继承AsyncTask,在类中实现异步操作,并提供接口反馈当前异步运行的程度(能够通过接口实现UI ...
- Servlet配置对象、上下文对象、多线程问题
一.Servlet配置对象(ServletConfig):Servlet初始化时,容器调用其init(ServletConfig)方法,传递该对象. 1.获得对象方法: (1).直接使用getServ ...
- sql: oracle, for update和for update nowait的区别
1. oracle for update和for update nowait的区别 http://www.cnblogs.com/quanweiru/archive/2012/11/09/276222 ...