自学内容网 自学内容网

TP5框架 之多对多关联

一、表设计

user表

group表

user_group表

二、模型设计

User模型

<?php

namespace app\index\model;

class Users extends \think\Model
{
    protected $pk = "id";

    protected $autoWriteTimestamp = "datetime";
    protected $dateFormat = 'Y-m-d H:i:s';

    protected $createTime = "date_entered";

    /**
     * 获取群列表
     * @return \think\model\relation\BelongsToMany
     */
    public function groups()
    {
        return $this->belongsToMany("Groups", "user_group", "group_id", "user_id");
    }
}

Group模型

<?php

namespace app\index\model;

use think\Model;

class Groups extends Model
{
    protected $pk = "id";
    protected $autoWriteTimestamp = "datetime";
    protected $dateFormat = 'Y-m-d H:i:s';

    protected $createTime = "date_entered";

    public function users()
    {
        return $this->belongsToMany("Users", "user_group", "user_id", "group_id");
    }
}


原文地址:https://blog.csdn.net/qq_27781261/article/details/143985988

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!