public function loadRelatedData() { if (IS_POST) { $items = $this->todo_model->select(); $items = DBUtil::loadRelatedData($items, 'user_id', 'user', 'id', array('userName' => 'user_name')); //$items = DBUtil::loadRelatedData($items,'user_id','user','id',array('userName'=>'user_name')); print_r($items); } }
public function get_pieces() { $user_id = I('post.user_id') ? I('post.user_id') : $this->user_id; $page = I('post.page'); $page = $page ? $page : 0; //$page = $page?$page-1:0; $visible = $user_id == $this->user_id ? '' : ' AND p.visible=1'; $piece_model = D('Piece'); /*$sql = "select count(c.comment_id) as comments_num,u.userName,u.avatar,p.piece_id,p.user_id,p.date,p.tag,p.content from hs_piece as p join hs_user as u on p.user_id=u.id left join hs_piece_comment as c on p.piece_id=c.piece_id where p.user_id=".$user_id.$visible." group by p.piece_id order by p.date desc limit ". $page*$this->piece_nums_per_page.",".$this->piece_nums_per_page; $pieces = $piece_model->query($sql); $pieces = $piece_model->alias('p')->where('p.user_id='.$user_id.$visible) ->field('u.id,u.userName,u.avatar,p.piece_id,p.date,p.tag,p.content') ->join('hs_user as u on u.id=p.user_id') ->order('p.date desc')->limit($page*$this->piece_nums_per_page,$this->piece_nums_per_page)->select();*/ $cached_pieces = S('User_pieces_' . $page . '_' . $user_id); //是否已有缓存 if (0) { $this->ajaxReturn(array('error' => 0, 'cached' => true, 'pieces' => $cached_pieces), 'json'); } else { $pieces = $piece_model->where('user_id=' . $user_id)->field('piece_id,user_id,date,tag,content,visible,visible_tag')->order('date desc')->select(); if ($pieces !== false) { //关联用户表 $pieces = DBUtil::loadRelatedData($pieces, 'user_id', 'user', 'id', array('userName', 'avatar')); //关联评论表 $pieces = DBUtil::loadRelatedData($pieces, 'piece_id', 'piece_comment', 'piece_id', array('count(comment_id) as comments_num')); $pieces = A('Auth')->filter_invisible_piece($pieces); //进行分页并缓存结果 $pieces = Util::pageOfArray($pieces, $page, $this->piece_nums_per_page); S('User_pieces_' . $page . '_' . $user_id, $pieces, array('type' => 'file', 'expire' => 5 * 60)); //缓存该用户第n页的碎片,5分钟 $this->ajaxReturn(array('error' => 0, 'pieces' => $pieces), 'json'); } else { $this->ajaxReturn(array('error' => 1, 'msg' => C('SITE_LANG.QUERY_FAILED')), 'json'); } } }
public function ng_view() { /** * 如果是公开文章则游客有浏览权限 * 私有文章则只能是登陆用户且是作者本身 */ $id = I('get.id'); /*$essay = $this->essay_model->field('hs_user.userName,hs_essay.user_id,hs_essay.essay_id,hs_essay.title,hs_essay.date,hs_essay.tag,hs_essay.content,hs_essay.visible')-> join('hs_user ON hs_user.id=hs_essay.user_id AND hs_essay.essay_id='.$id)->find(); if($essay === false){$this->ajaxReturn(array('error'=>1,'msg'=>C('SITE_LANG.QUERY_FAILED')),'json');}//查询失败 else if($essay == NULL){$this->ajaxReturn(array('error'=>2,'msg'=>C('SITE_LANG.ESSAY_NOT_EXIST')),'json');}//文章不存在 else{ if($essay['visible'] == 0 && $essay['user_id'] != $this->user_id) $this->ajaxReturn(array('error'=>1,'msg'=>C('SITE_LANG.ESSAY_VIEW_ACCESS_DENIED')),'json');//没有查看的权限 else if($essay['visible'] == 1 || ($essay['visible'] == 0 && $essay['user_id'] == $this->user_id)){//公开的文章 //check if essay comment open $user_config = A('User')->get_user_config($essay['user_id']); $privacy_essay_comment = $user_config['privacy_essay_comment']==1?true:false; $essay_comment_on = $privacy_essay_comment && C('ESSAY_COMMENT_ON');//是否开启了评论 //get essay comments if(empty($_SESSION['USER_ID'])){//如果未登录 $comments = array(); $comments_num = count($comments); //$essay_comment_on = false; }else{ $comments = A('Comment')->get_essay_comments($essay['essay_id']); $comments_num = count($comments); } $res = array('error'=>0,'essay'=>$essay,'comments'=>$comments,'comments_num'=>$comments_num,'essay_comment_on'=>$essay_comment_on,'is_logined'=>empty($_SESSION['USER_ID'])?0:1); $this->ajaxReturn($res,'json'); } }*/ $essays = $this->essay_model->where('essay_id=' . $id)->field('user_id,essay_id,title,date,tag,content,visible,visible_tag')->select(); //$this->ajaxReturn(array('error'=>1,'essay'=>$essays),'json'); if ($essays !== false) { if ($essays == NULL) { $this->ajaxReturn(array('error' => 2, 'msg' => C('SITE_LANG.ESSAY_NOT_EXIST')), 'json'); } //文章不存在 $essays = A('Auth')->filter_invisible_essay($essays); //可见性验证 if (count($essays)) { //关联用户表 $essay = DBUtil::loadRelatedData($essays, 'user_id', 'user', 'id', array('userName'))[0]; //检查是否开启了文章评论,若开启则获取文章评论 $user_config = A('User')->get_user_config($essay['user_id']); $essay_comment_on = $user_config['privacy_essay_comment'] == 1 && C('ESSAY_COMMENT_ON'); //是否开启了评论 //获取文章评论 if (empty($_SESSION['USER_ID'])) { //如果未登录 $comments = array(); $comments_num = 0; } else { $comments = A('Comment')->get_essay_comments($essay['essay_id']); $comments_num = count($comments); } $res = array('error' => 0, 'essay' => $essay, 'comments' => $comments, 'comments_num' => $comments_num, 'essay_comment_on' => $essay_comment_on, 'is_logined' => empty($_SESSION['USER_ID']) ? 0 : 1); $this->ajaxReturn($res, 'json'); } else { $this->ajaxReturn(array('error' => 1, 'msg' => C('SITE_LANG.ESSAY_VIEW_ACCESS_DENIED')), 'json'); } //没有查看的权限 } else { $this->ajaxReturn(array('error' => 1, 'msg' => C('SITE_LANG.QUERY_FAILED')), 'json'); } //查询失败 }