继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

五十二章 TP51之关联模型

手记佰篇
关注TA
已关注
手记 60
粉丝 58
获赞 272

一对多关联

<?php
namespace app\index\model;

use think\Model;

class Article extends Model
{
   public function comments()
   {
       return $this->hasMany('Comment','art_id','id');

}

}

}
关联查询

我们可以通过下面的方式获取关联数据

$article = Article::get(1);
// 获取文章的所有评论
dump($article->comments);
// 也可以进行条件搜索
dump($article->comments()->where('status',1)->select());

根据关联条件查询

可以根据关联条件来查询当前模型对象数据,例如:

// 查询评论超过3个的文章
$list = Article::has('comments','>',3)->select();
// 查询评论状态正常的文章
$list = Article::hasWhere('comments',['status'=>1])->select();


打开App,阅读手记
0人推荐
发表评论
随时随地看视频慕课网APP