还记得第一次用ThinkPHP的relation,做了一个关联查询,觉得特别好用。有那么一天尝试着用关联插入,怎么插,都插不进,我插,我擦!

后来在龙哥的指点下算是成功的实践了一次,后来怎么用都不顺,后来变远离了 relation,便觉得这是TP 本身的问题,却不知是自己没有找到问题的症结,还是编程届

的那句谚语说的好,你没有了解出现问题的真正原因,就不算解决了BUG。

最近公司做接口,两张表一对多的关系, 正常查询的话(select tag.*, evaluate.* from tbl_tag as tag,tbl_evaluate as evaluate where(tag.tag_id = evaluate.tag_id))是多条记录,而需要返回如下的JSON数据:

{
"ret": "0",
"msg": "ok",
"data": [
{
"tag_id": "1226",
"name": "软件故障类",
"type": "1",
"Icon": "/style/images/sortImgs/small/",
"created_at": "1373383665",
"status": "1",
"explain": "暂无",
"changed_at": "1373383665",
"evaluate_count": "5",
"contents_type": "1",
"process_id": null,
"evaluate": [
{
"evaluate_id": "4",
"tag_id": "1226",
"name": "任务创建时间",
"limit_time": "0",
"alert_time": "0",
"position": "1",
"description": null,
"isfixed": null,
"status": null,
"remark": null
},
{
"evaluate_id": "5",
"tag_id": "1226",
"name": "任务分配时间",
"limit_time": "10",
"alert_time": "5",
"position": "1",
"description": null,
"isfixed": null,
"status": null,
"remark": null
},
{
"evaluate_id": "6",
"tag_id": "1226",
"name": "任务领取时间",
"limit_time": "15",
"alert_time": "3",
"position": "1",
"description": null,
"isfixed": null,
"status": null,
"remark": null
},
{
"evaluate_id": "7",
"tag_id": "1226",
"name": "任务开始时间",
"limit_time": "60",
"alert_time": "20",
"position": "1",
"description": null,
"isfixed": null,
"status": null,
"remark": null
},
{
"evaluate_id": "8",
"tag_id": "1226",
"name": "任务结束时间",
"limit_time": "240",
"alert_time": "30",
"position": "1",
"description": null,
"isfixed": null,
"status": null,
"remark": null
}
]
},
{
"tag_id": "1229",
"name": "改进后的任务我22",
"type": "1",
"Icon": "/style/images/sortImgs/small/",
"created_at": "1373700085",
"status": "1",
"explain": null,
"changed_at": "1373700979",
"evaluate_count": "5",
"contents_type": "1",
"process_id": null,
"evaluate": [
{
"evaluate_id": "10",
"tag_id": "1229",
"name": "时间1",
"limit_time": "11",
"alert_time": "6",
"position": "1",
"description": null,
"isfixed": null,
"status": null,
"remark": null
}
]
},
{
"tag_id": "1230",
"name": "维修任务修改2",
"type": "1",
"Icon": "/style/images/sortImgs/small/",
"created_at": "1373714061",
"status": "1",
"explain": null,
"changed_at": "1373715337",
"evaluate_count": "2",
"contents_type": "1",
"process_id": null,
"evaluate": [
{
"evaluate_id": "11",
"tag_id": "1230",
"name": "执行时间1",
"limit_time": "4",
"alert_time": "3",
"position": "1",
"description": null,
"isfixed": "1",
"status": "1",
"remark": null
},
{
"evaluate_id": "12",
"tag_id": "1230",
"name": "执行时间1",
"limit_time": "5",
"alert_time": "4",
"position": "2",
"description": null,
"isfixed": "1",
"status": "1",
"remark": null
}
]
}
]
}

阅读别人的代码的时候,发现TP的  relation能返回这样的数据,好了,躲不掉的Relation

马上我就又凌乱了,之前的TP直接继承RelationModel,关联查询  so  easy, 关键是公司继承了BaseModel,各种研究,各种陌生

/**
* 通过id查询标签
*/
public function show(){

		//自动校验
$dr_tag=D('Tag');
$condition=$dr_tag->create($_GET);
if( false === $condition) exit(RS::jsonReturn($dr_tag->getError())); $dr_process = D('Process');
$dr_process = $dr_process->switchModel ( 'Relation' );
$linklist = ProcessModel::get_link ( 'this' );
$dr_process->setProperty ( '_link', $linklist ); $field = TagModel::getObjFields ( 'this' ); $tags_id=$condition['tags_id'];
$name=$condition['name'];
$explain=$condition['explain'];
if(empty($tags_id) && empty($name) && empty($explain)) exit(RS::jsonReturn("20001")); //查询条件
if (!empty ( $tags_id)) $where ['tbl_tag.tag_id'] = array ('eq', $condition['tags_id'] );
if (!empty ( $condition ['name'] )) $where ['tbl_tag.name'] = array ('eq', $name );
if (!empty ( $condition ['explain'] )) $where ['tbl_tag.explain'] = array ('like','%'.$explain.'%');
$where['tbl_tag.tag_id'][]=array('neq',999);
$field[] = 'tbl_tag.tag_id';
$field[] = 'tbl_tag.name';
$field[] = 'tbl_tag.type';
$field[] = 'tbl_tag.status';
$field[] = 'tbl_tag.Icon';
$field[] = 'tbl_tag.created_at';
$field[] = 'tbl_tag.changed_at';
$field[] = 'tbl_tag.explain';
$field[] = 'tbl_tag.contents_type';
$field[] = 'tbl_tag.process_id';
$field[] = 'tbl_process.process_count'; $data = $dr_process->field ( $field )
->join ( 'right join tbl_tag ON tbl_tag.process_id = tbl_process.process_id' )
->where ( $where )
->relation ( true )
->find(); //返回数据
if(empty($data)) exit(RS::jsonReturn("20000"));
echo RS::jsonReturn("0",$data,"ok"); }

照猫画虎,自己来一个,尽管搞不明白:

$linklist = ProcessModel::get_link ( 'this' );
$dr_process->setProperty ( '_link', $linklist );

//关联,显示所有任务类型和指标详情
protected function tagsShow_link(){
return array(
'evaluate'=>array(
'mapping_type' => HAS_MANY,
'class_name'=>'Evaluate',
'foreign_key'=>'tag_id',
//'mapping_name' =>'indexs',
'mapping_fields_name' =>'this'
),
); } /* 返回字段 */
public static function tagsShowObjFields(){
$ObjFields = self::$allObjFields;
foreach ($ObjFields as $key=>$val){
if('notifications_enabled' === $val) unset($ObjFields[$key]);
}
return $ObjFields;
}

结果就是出不来结果,Relation不出来,出来的就是关联的是false

源于不明白这个get_link和  setProperty总是以为是这里出了批漏,各种查,各种对比,各种纠结,一个人,在周六

$linklist = ProcessModel::get_link ( 'this' );
$dr_process->setProperty ( '_link', $linklist );

找到了一条靠谱的解决方法,是改RelationModel下面的M 方法为D ( http://lhdeyx.blog.163.com/blog/static/318196972010112351516763/),我打开一看,晕死了,就是D;

之前都是在物理上实现外键约束的,现在是在逻辑上实现,以为Relation关联不上错在这里,实际上是无关的

无奈了,最后,想SQL语句,一开始就想到了,下面的,觉得可行,就是效率太低,PASS

$tag = D('Tag');
$evaluate = D('Evaluate');
$tag_ids = $tag->getField('tag_id,name');
if(isset($_GET['since_at']) && !empty($_GET['since_at'])){
$since_at = $_GET['since_at'];
$condition['changed_at'] = array('gt',$since_at);
}
//echo $tag->getLastSQl();die();
foreach($tag_ids as $key=>$val){
$map['tag_id'] = array('eq',$key);
$condition['tag_id'] = array('eq',$key);
$result = $tag->where($condition)->find();
$res = $evaluate->where($map)->select();
$result['indexs'] = $res;
$data[] = $result;
}
//返回数据
if(empty($data)) exit(RS::jsonReturn("20000"));
echo RS::jsonReturn("0",$data,"ok");

后来就google大神,论坛,以至于差点用起了行列转换

--行列转换模型
select name 姓名,
max(case subject when '语文' then result else 0 end) 语文,
max(case subject when '数学' then result else 0 end) 数学,
max(case subject when '物理' then result else 0 end) 物理
from tb
group by name

纠结在继续,

在sql搞不定之后,回到了Relation,还是搞不定,夜很静了,我只能简单粗暴了,一开始的sql.

…………

方姐,来了,观察了一下情况,该有的都有了,于是

$data = $dr_tag->relation(true)->select();
$evaluate = D('Evaluate');
echo $evaluate->getLastSql();die('dd');

我一下就反映过来了,告诉TA是limit_time,后来改了Model中的字段

sudo rm -rf  *,TA迷人的样子让我折服,轻轻的在我耳边说,该了Model要删缓存,Ta 说的那么轻描淡写,我觉永远记住了

我之前也

echo $dr_tag->getLastSql();die('dd');提示的错误让人更纠结。回想Ta 解决BUG缜密思路,我…………
提示你false, 我永远都不知道用被关联的表evaluate实例化后输出,直到Ta 展示给我
爱上了Relation,  爱上……

[置顶] 让我爱恨的ThinkPHP Relation的更多相关文章

  1. 爱pia戏推出PC客户端,为您自动置顶窗口,方便查找

    爱pia戏推出PC客户端, 可以在无法使用插件的时候,使用PC客户端, 将为您自动置顶窗口,方便查看剧本. 百度网盘下载地址: 链接: http://pan.baidu.com/s/1pLpvn5p ...

  2. Menu与ActionBar的爱恨情仇

    最近在开发一款音乐播放器,在开发过程中遇到了一点小麻烦,通过android API搞清楚了Menu与ActionBar的爱恨情仇,写了个小Demo祭奠一下那些年我们陷进去的坑,有不对的地方请大神们批评 ...

  3. 在UWP中页面滑动导航栏置顶

    最近在研究掌上英雄联盟,主要是用来给自己看新闻,顺便copy个界面改一下段位装装逼,可是在我copy的时候发现这个东西 当你滑动到一定距离的时候导航栏会置顶不动,这个特性在微博和淘宝都有,我看了@ms ...

  4. WinFrom窗体始终置顶

    调用WindowsAPI使窗体始终保持置顶效果,不被其他窗体遮盖: [DllImport("user32.dll", CharSet = CharSet.Auto)] privat ...

  5. web移动端fixed布局和input等表单的爱恨情仇 - 终极BUG,完美解决

    [问题]移动端开发,ios下当fixed属性和输入框input(这里不限于input,只要可以调用移动端输入法的都包括,如:textarea.HTML5中contenteditable等),同时存在的 ...

  6. winform窗体置顶

    winform窗体置顶 金刚 winform 置顶 今天做了一个winform小工具.需要设置置顶功能. 网上找了下,发现百度真的很垃圾... 还是必应靠谱些. 找到一个可以链接. https://s ...

  7. 自定义置顶TOP按钮

    简述一下,分为三个步骤: 1. 添加Html代码 2. 调整Css样式 3. 添加Jquery代码 具体代码如下: <style type="text/css"> #G ...

  8. ahk之路:利用ahk在window7下实现窗口置顶

    操作系统:win7 64位 ahk版本:autohotkey_L1.1.24.03 今天安装了AutoHotkey_1.1.24.03.SciTE.PuloversMacroCreator,重新开始我 ...

  9. Qt中让Qwidget置顶的方法

    一般来是说窗体置顶和取消只要        setWindowFlags(Qt::WindowStaysOnTopHint);        setWindowFlags(Qt::Widget); 要 ...

随机推荐

  1. ASP.NET MVC应用程序把文字写在图片上

    原文:ASP.NET MVC应用程序把文字写在图片上 Insus.NET实现这篇<MVC把随机产生的字符串转换为图片>http://www.cnblogs.com/insus/p/3624 ...

  2. uptime命令具体解释——linux性能分析

    基本使用: uptime [-V] 实际分析: 这里因为命令比較简单,所以我们不进行具体地介绍.但越是简单的命令,就越是好用方便.像之前百度面试运维的时候,面试管问我哪个命令能够看得到系统负载.我立即 ...

  3. Javascript--dataTransfer

    描述: 提供对于预定义的剪贴板格式的访问,以便在拖拽中使用 属性 描述 参数 dropEffect[=sCursorStyle] 设置或获取拖拽操作的类型和要显示的光标类型 可选的copy 复制样式被 ...

  4. SQL点滴13—收集SQLServer线程等待信息

    原文:SQL点滴13-收集SQLServer线程等待信息 要知道线程等待时间是制约SQL Server效率的重要原因,这一个随笔中将学习怎样收集SQL Server中的线程等待时间,类型等信息,这些信 ...

  5. Spring MVC 的 研发之路

    翻译器:intellij idea 一个.创建spring mvcproject   一个. 二. 三. watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcX ...

  6. PL/SQL Developer 连接Oracle数据库详细配置方法

    PL/SQL Developer 连接Oracle数据库详细配置方法 近段时间很多网友提出监听配置相关问题,客户终端(Client)无法连接服务器端(Server).本文现对监听配置作一简单介绍,给出 ...

  7. 删除sql server中重复的数据

    原文:删除sql server中重复的数据 with list_numbers as( select Name, AuthorOrTime, Url, Price, EstimatePrice, Si ...

  8. 如何在在网页上显示pdf文档

    ------解决方案--------------------通过flash插件 ------解决方案--------------------RAD PDF Release 2.7 http://www ...

  9. EF的四种开发模式

    EF提供了四种开发模式,具体如下:(转载)Code First(New DataBase) :在代码中定义类和映射关系并通过model生成数据库,使用迁移技术更新数据库.Code First(Exis ...

  10. Visual Studio 2014

    开发 ASP.NET vNext 初步总结(使用Visual Studio 2014 CTP1) 2014-06-06 18:04 by 梁逸晨, 2149 阅读, 29 评论, 收藏, 编辑 新特性 ...