1.需求描述

首页文章列表,需要同时获取文章的点赞和被关注数,同时如果被当前用户点赞或关注了会显示相应小图标进行区别。图示如下:

2.解决方案

数据库设计:

文章对应Article表,其中包括收藏数字段fav,点赞数字段zan

中间表user_fav,user_zan分别表示某一用户对某篇文章的点赞与收藏, 其中包括userid,usertype,article_id等字段

TP5代码实现部分:

框架用的TP5,参考开发手册其中关联模型部分,在article模型中增加userFav和userZan方法

    public function userFav()
{
return $this->hasMany('UserFav','article_id')->field('userid');
} public function userZan()
{
return $this->hasMany('UserZan','article_id')->field('userid');
}

Controller中index方法显示文章列表

    public function index($page=1)
{
// $this->articlemodel = new Article();
// $list = $this->articlemodel->where('schoolid',$user['schoolid'])->where('classid',$user['classid'])->group('create_time')->select();
$list = $this->articlemodel->withCount(['userzan'=>function($query){
$query->where('userid',cookie('userid'))->where('usertype',cookie('usertype'));
}])->order('zan desc,is_top desc,is_recommend desc,id desc')->paginate(5,false,['page' => $page]); $this->assign('list', $list);
return $this->fetch();
}

View中获取点赞数关注数同时判断如果当前用户点赞,部分代码

    {volist name="list" id="vo"}
<div class="in_parent">
<img src="{$vo['photo'][0]}" data-id="{$vo['id']}">
<div class="in_par_con">
<p>{$vo.title}</p>
</div>
<div class="in_par_con">
{foreach :explode(',',$vo['tags']) as $tag}
<span>{$tag}</span>
{/foreach}
</div>
<div class="in_per">
<div class="in_per_lf fl">
<img src="__H5IMAGES__/pic.png" class="fl"/>
<div class="fl ml5 mt10">{$vo.author}<p class="gray">{php}echo date('Y-m-d',strtotime($vo['create_time']));{/php}</p></div>
</div>
<div class="in_per_rt fr">
<span class="gray fr mt30 ml3 care-val">收藏 {$vo.fav}</span>
<div class="fr mt30 ml3 care{eq name='$vo.userfav_count' value='1'} care01{/eq}" data-id="{$vo.id}"></div>
<span class="gray fr mt30 ml3 zan-val">{$vo.zan}</span>
<div class="fr mt30 ml3 zan{eq name='$vo.userzan_count' value='1'} zan01{/eq}" data-id="{$vo.id}"></div>
</div>
<div class="clear"></div>
</div>
</div>
{/volist}

php用的时间不长,用的不对的地方望不吝赐教

Thinkphp关联模型使用的更多相关文章

  1. ThinkPHP 关联模型(二十)

    原文:ThinkPHP 关联模型(二十) ThinkPHP关联模型 两表关联查询:Message 和  user  关联条件uid(参考手册:模型->关联模型) 步骤: 一:创建Message表 ...

  2. ThinkPHP关联模型详解

    在ThinkPHP中,关联模型更类似一种mysql中的外键约束,但是外键约束更加安全,缺点却是在写sql语句的时候不方便,ThinkPHP很好得解决了这个问题.但是很多人不动关联模型的意思.现在就写个 ...

  3. ThinkPHP关联模型如何关联非主键

    ThinkPHP关联模型默认是主键外键关联 官方并没有提供相关文档 如何实现非主键与非主键间之间的关联 <?php namespace Admin\Model; use Think\Model\ ...

  4. thinkphp 关联模型配置代码

    <?php /** * 公司与部门关联模型 */ class CompanyRelationModel extends RelationModel{ //主表名称 protected $tabl ...

  5. ThinkPHP 关联模型中查询某条记录的父级(非查询子级)

    数据表 id      cat_name      cat_pid 76     手机.数码     0 84     手机配件        76 86     蓝牙耳机        84 从属关 ...

  6. ThinkPHP - 关联模型 - 多对多

    表结构: 映射关系: UserRelationModel会取UserRelation为表名称.所以要自定义表名称: //定义主表名称protected $tableName = 'User'; < ...

  7. Thinkphp 关联模型和试图模型区别

    关联模型主要在多表操作时使用,比如 user表,user_role表,role表 user_role字段:uid,rid,它作为中间表,负责将user和role之间的,1对1,1对多,多对多的关系进行 ...

  8. Thinkphp 关联模型

    1.定义关联模型 2.使用关联模型 D('BlogRelation')->relation(true)->add($data);

  9. ThinkPHP - 关联模型 - 一对多

    使用之前,先引入文件夹,否则相应的功能不能实现. 如果对thinkPHP不精通,使用或开发的时候,最好直接使用完成版本的ThinkPHP. 关系模型定义: <?php /** * 继承自 Rel ...

随机推荐

  1. 作业要求20181113-4 Beta阶段第1周/共2周 Scrum立会报告+燃尽图 03

    作业要求:https://edu.cnblogs.com/campus/nenu/2018fall/homework/2385 版本控制:[https://git.coding.net/lglr201 ...

  2. MathExamV2.0四则混合运算计算题生成器

    MathExamV2.0四则混合运算计算题生成器----211606360 丁培晖 211606343 杨宇潇 一.预估与实际 PSP2.1 Personal Software Process Sta ...

  3. 1.2Linux下C语言开发基础(学习过程)

    ===============第二节  Linux下C语言开发基础=========== ********************** 重要知识点总结梳理********************* 一 ...

  4. c# webBrowser清除缓存问题

    1.webBrowser的浏览器为ie. 2.通过清除ie缓存即可. 3.代码调用如下: public enum ShowCommands : int { SW_HIDE = , SW_SHOWNOR ...

  5. UVALive - 6893 The Big Painting 字符串哈希

    题目链接: http://acm.hust.edu.cn/vjudge/problem/129730 The Big Painting Time Limit: 5000MS 题意 给你一个模板串和待匹 ...

  6. rsyslog配置文件详解(rsyslog.conf)

    # rsyslog configuration file # For more information see /usr/share/doc/rsyslog-*/rsyslog_conf.html # ...

  7. inotify 工具 是一种强大的、细粒度的、异步文件系统监控机制

    前言:Inotify是一种强大的.细粒度的.异步文件系统监控机制,它满足各种各样的文件监控需要,可以监控文件系统的访问属性.读写属性.权限属性.删除创建.移动等操作,也就是可以监控文件发生的一切变化. ...

  8. 【Leetcode】445. Add Two Numbers II

    You are given two non-empty linked lists representing two non-negative integers. The most significan ...

  9. MySQL误操作后如何快速回滚(转)

    本文转自http://www.cnblogs.com/dfcao/p/6147970.html#undefined 感谢作者 基本上每个跟数据库打交道的程序员(当然也可能是你同事)都会碰一个问题,My ...

  10. MATLAB串口操作和GUI编程

    程序说明 V1.0 2015/2/08 MATLAB串口操作和GUI编程   概述   本文介绍了程序AD9512_Serial_GUI的编程思路和功能.该程序设计到MATLAB的图像用户界面编程的基 ...