ITPub博客

首页 > IT职业 > IT职场 > 五十二章 TP51之关联模型

五十二章 TP51之关联模型

IT职场 作者:zhaozy78 时间:2021-09-09 09:47:30 0 删除 编辑

一对多关联


namespace appindexmodel;

use thinkModel;

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();


来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/3276/viewspace-2812429/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2019-08-14

  • 博文量
    72
  • 访问量
    426