惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

S
Secure Thoughts
Security Latest
Security Latest
Simon Willison's Weblog
Simon Willison's Weblog
O
OpenAI News
GbyAI
GbyAI
L
LINUX DO - 最新话题
A
Arctic Wolf
T
Tor Project blog
G
GRAHAM CLULEY
I
InfoQ
博客园_首页
IT之家
IT之家
The Register - Security
The Register - Security
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
P
Proofpoint News Feed
The GitHub Blog
The GitHub Blog
Blog — PlanetScale
Blog — PlanetScale
N
Netflix TechBlog - Medium
K
Kaspersky official blog
博客园 - 三生石上(FineUI控件)
S
SegmentFault 最新的问题
U
Unit 42
PCI Perspectives
PCI Perspectives
量子位
P
Palo Alto Networks Blog
S
Securelist
T
Troy Hunt's Blog
博客园 - 【当耐特】
Recorded Future
Recorded Future
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
S
Security Affairs
Engineering at Meta
Engineering at Meta
T
The Blog of Author Tim Ferriss
博客园 - 聂微东
罗磊的独立博客
N
News and Events Feed by Topic
人人都是产品经理
人人都是产品经理
B
Blog RSS Feed
NISL@THU
NISL@THU
C
Cisco Blogs
T
Threatpost
有赞技术团队
有赞技术团队
Forbes - Security
Forbes - Security
Hugging Face - Blog
Hugging Face - Blog
Last Week in AI
Last Week in AI
T
The Exploit Database - CXSecurity.com
Cloudbric
Cloudbric
Cyberwarzone
Cyberwarzone
Google DeepMind News
Google DeepMind News
C
Cyber Attacks, Cyber Crime and Cyber Security

博客园 - s1ihome

弹框居中的方法 如何 clone git 项目到一个非空目录 Uiautomatorviewer报错:Unexpected error while obtaining UI hierarchy java.lang.reflect.InvocationTargetException 常见银行卡号账号长度参考表 TP5.0 数据库查询is not null vscode 中sftp配置 移动端H5上传图片并压缩上传 centos 7 pdo thinkphp 5 where 组合条件map数组or [转]处理上百万条的数据库如何提高处理查询速度 连接oracle读取数据 SQL SERVER批量修改表名前缀 php导出excel java开发微信公众平台备忘 AngularJS小试牛刀 spring mvc开发过程知识点记录 微信公众平台项目中遇到的小问题40016,Invalid button size Dynamic Virtual Channels 木马的隐藏方式
Thinkphp关联模型使用
s1ihome · 2017-10-09 · via 博客园 - s1ihome

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用的时间不长,用的不对的地方望不吝赐教