写一个简单的TP5一对多关联吧

浏览:12779 发布日期:2016/12/27 分类:ThinkPHP5专区 关键字: 模型,TP5,一对多
article 文章表 id 主键
comment 文章表 comment_id 主键 id 文章表关联主键

index/model/Article.php<?php 
namespace app\index\model;
use think\Model;
class Article extends Model
{
    public function comment()
    {
        return $this->hasMany('comment','id','id');
    }
}

 ?>
index/model/Comment.php<?php 
namespace app\index\model;
use think\Model;
class Comment extends Model
{
    public function article()
    {
        return $this->belongsTo('article');
    }
}

 ?>
控制器内代码use app\index\model\Article;

//查询所有每篇文章下的评论内容且分页
$article_list = Article::with('comment')->order('add_time DESC')->field('id,title,add_time,introduction,clicknum')->paginate(20);
最佳答案
评论( 相关
后面还有条评论,点击查看>>