Home > PHP Framework > ThinkPHP > How to use the tp5 framework to implement multi-database queries

How to use the tp5 framework to implement multi-database queries

王林
Release: 2020-03-04 17:56:06
forward
2774 people have browsed it

How to use the tp5 framework to implement multi-database queries

Foreword:

Sometimes a management background needs to involve multiple databases. For example, mall management, live broadcast management, message management, etc., they all have their own databases. At this time, you need to connect to multiple databases and process them. thinkphp can support multiple database connections.

How to deal with it?

1. Configure multiple databases

The database information in database.php will be connected by default.

<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
return [
  // 数据库类型
  &#39;type&#39;      => &#39;mysql&#39;,
  // 服务器地址
  &#39;hostname&#39;    => &#39;&#39;,
  // 数据库名
  &#39;database&#39;    => &#39;&#39;,
  // 数据库用户名
  &#39;username&#39;    => &#39;&#39;,
  // 数据库密码
  &#39;password&#39;    => &#39;&#39;,
  // 数据库连接端口
  &#39;hostport&#39;    => &#39;3306&#39;,
  // 数据库编码默认采用utf8
  &#39;charset&#39;     => &#39;&#39;,
  // 数据库表前缀
  &#39;prefix&#39;     => &#39;&#39;
];
Copy after login

tp5 will automatically load database.php

(Recommended tutorial: thinkphp tutorial)

We can create a few more in the extra folder Configuration of other databases, such as database_mall, database_live, database_app, etc.

2. Initialization

Initialize in the model module

<?php
namespace app\admin\model;
use think\Model;
use think\Db;
class LiveRecharge extends Model
{
  protected $db_app;
  function __construct()
  {
    $this->db_app = Db::connect(&#39;database_app&#39;);
  }
}
Copy after login

3. Use

$this->db_app->table(&#39;order&#39;)->select();
Copy after login

so that you can query data in other databases.

The following is the complete code:

<?php
namespace app\admin\model;
use think\Model;
use think\Db;
class LiveRecharge extends Model
{
  protected $db_app;
  function __construct()
  {
    $this->db_app = Db::connect(&#39;database_app&#39;);
  }
  // 获取分页
  public function getList($customer_id = &#39;&#39;,$nickname = &#39;&#39;,$paytime = &#39;&#39;,$pagesize = &#39;&#39;)
  {
    $pagesize = $pagesize && $pagesize > 0 ? $pagesize : config(&#39;default_page_size&#39;);
    $where = array();
    $where[&#39;o.type&#39;] = 3;
    if ($customer_id) {
      $where[&#39;o.uid&#39;] = $customer_id;
    }
    if ($nickname) {
      $where[&#39;c.NickName&#39;] = [&#39;like&#39;,&#39;%&#39;.$nickname.&#39;%&#39;];
    }
    if ($paytime) {
      $where[&#39;o.addtime&#39;] = array([&#39;>&#39;,$paytime.&#39; 00:00&#39;], [&#39;<&#39;,$paytime.&#39; 23:59&#39;]);
    }
    $result = $this->db_app->table(&#39;order&#39;)
      ->alias(&#39;o&#39;)
      ->where($where)
      ->join(&#39;customer c&#39;,&#39;o.uid = c.Id&#39;)
      ->field(&#39;o.*,c.NickName as nickname&#39;)->paginate($pagesize,false,[
        &#39;query&#39; => [
          &#39;customer_id&#39;=>$customer_id,
          &#39;nickname&#39;=>$nickname,
          &#39;paytime&#39;=>$paytime
        ]
      ]);
    $page = $result->render(); // 分页
    $data = $result->all(); // 数据
    foreach ($data as $k=>$v) {
      $data[$k][&#39;diamond&#39;] = intval($v[&#39;money&#39;])*10;
    }
    //    dump($this->db_app->getLastSql());
    $total_diamond = $this->db_app->table(&#39;order&#39;)->where(&#39;type&#39;,3)->sum(&#39;money*10&#39;);
    $outData[&#39;page&#39;] = $page;
    $outData[&#39;data&#39;] = $data;
    $outData[&#39;total_diamond&#39;] = $total_diamond;
    return $outData;
  }
}
Copy after login

For more programming-related content, please pay attention to the Programming Introduction column on the php Chinese website!

The above is the detailed content of How to use the tp5 framework to implement multi-database queries. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:jb51.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template