thinkphp5本地数据库表和外接数据库表一对多问题,求大神来解决

为什么我做的本地表和其他服务器数据库表关联,如果把数据库配置项写在配置文件里怎么查他都是查其他服务器数据库表中的一张,但是把配置项写在模型里又是正常可以使用.


这是本地表模型,一对多关联外接表

<?php

namespace app\admin\model;

use think\Model;

class Wxuser extends Model{
    protected $table = 'qgs_admin';
    protected $hidden = ['pwd','name','email','headimg','createtime'];
    protected $pk = 'id';
    public function wxshop_panel(){
        return $this->hasOne('WxshopPanel','qgs_admin_id')->field(['url','id','title','img']);
    }    

    public function shopuser_behaviors(){
        return $this->hasMany('ShopuserBehavior','qgs_admin_id');
    }    

    public function product_carts(){
        return $this->hasMany('ProductCart','token','token');
    }
    public function product_cart(){
        return $this->hasOne('ProductCart','token','token');
    }    
    public function product_shop_parcel(){
        return $this->hasOne('ProductShopParcel','rid','id');
    }
}

这事其中外接表的模型,然后其他外接表一样这样写法

<?php

namespace app\admin\model;
use think\Model;
use SoftDelete;

class ProductShopParcel extends Model{
    protected $autoWriteTimestamp = 'datetime';
    protected $deleteTime = 'delete_time';
    protected $table = 'wy_product_shop_parcel';
    protected $connection = [
        // 数据库类型
        'type'        => 'mysql',
        // 服务器地址
        'hostname'    => '',
        // 数据库名
        'database'    => 'wxtest',
        // 数据库用户名
        'username'    => 'wxtest',
        // 数据库密码
        'password'    => '',
        // 数据库编码默认采用utf8
        'charset'     => 'utf8',
        // 数据库表前缀
        'prefix'      => 'wy_',
        // 数据库调试模式
        'debug'       => false,
    ];

    // function __construct(){
    //     $this->connection = Config('olddb');
    // }

}

这样是可以正常使用的,但如果我把这配置项写到配置文件后,用Config('配置项名')在方法里写,调用模型类,他只会随意读回第一张外接表,很奇怪,求指导

阅读 3.3k
1 个回答

但如果我把这配置项写到配置文件后,用Config('配置项名')在方法里写

你是怎么写的啊?

如果你不把配置写在model里面,还可以

Db::connect('db_config')
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题