1. 程式人生 > >tp5中操作mongdb資料庫

tp5中操作mongdb資料庫

開啟php.ini 新增:

extension=php_mongodb.dll

windows環境下檢視是否安裝成功

php -m|findstr mongodb
mongodb

然後使用Composer安裝ThinkPHP5.0的MongoDb驅動:

composer require topthink/think-mongo=1.*

檢視目錄

在根目錄下增加資料庫配置檔案mongodb.php中的:

return [
    // 資料庫型別
    'type' => '\think\mongo\Connection',
    // 伺服器地址
    'hostname' => '127.0.0.1',
    // 資料庫名
    'database' => 'test',
    // 使用者名稱
    'username' => '',
    // 密碼
    'password' => '',
    // 埠
    'hostport' => '27017',
];

在應用目錄新建app\mongodb\model\MongodbConnection.php

<?php
namespace app\mongdb;
use think\Config;
use think\Model;
/**
 * Created by WSG.
 * Date: 2018/8/22
 * Time: 21:12
 */
abstract class MongdbConnection extends Model {

     protected $connection = [];

    /**
     * 建立模型的查詢物件
     * @access protected
     * @return Query
     */
    protected function buildQuery()
    {        
        //載入配置檔案
        Config::load(APP_PATH . 'mongodb.php', 'mongodb');
        $connection = Config::get('mongodb');
        // 合併資料庫配置
        if (!empty($this->connection) && is_array($this->connection)) {
            $connection = array_merge($connection, $this->connection);
        }
        $con = \think\Db::connect($connection);
        // 設定當前模型 確保查詢返回模型物件
        $queryClass = $this->query ?: $con->getConfig('query');
        $query      = new $queryClass($con, $this);

        // 設定當前資料表和模型名
        if (!empty($this->table)) {
            $query->setTable($this->table);
        } else {
            $query->name($this->name);
        }

        if (!empty($this->pk)) {
            $query->pk($this->pk);
        }
        return $query;
    }

}

在應用目錄新建app\mongodb\model\User.php

<?php
namespace app\mongodb;
/**
 * Created by WSG.
 * Date: 2018/8/22
 * Time: 21:12
 */
class User extends MongodbConnection {

   public function gets() {
        $data = [
            'user_id' => 23222,
            'name' => '2344wew',
            'username' => '33r54r'
        ];
        $a = $this->insert($data);
       $a = $this->all();
       dump(collection($a)->toArray());exit;
   }
}

控制器呼叫

//控制器呼叫
<?php
namespace app\index\controller;
/**
 * Created by WSG.
 * Date: 2018/8/17
 * Time: 10:22
 */
class Index {


  public function index() {
    // 讀取一條資料
        $a  = new \app\mongdb\User();
        $a->gets();
  }

}
array(5) {
  [0] => array(4) {
    ["user_id"] => int(1)
    ["name"] => string(4) "wggg"
    ["username"] => string(3) "wgg"
    ["id"] => string(24) "5b7d5f027bf16251d4003d51"
  }
  [1] => array(4) {
    ["user_id"] => int(2)
    ["name"] => string(4) "wggg"
    ["username"] => string(3) "wgg"
    ["id"] => string(24) "5b7d5f027bf16251d4003d53"
  }
  [2] => array(4) {
    ["user_id"] => int(122)
    ["name"] => string(7) "2344wew"
    ["username"] => string(6) "33r54r"
    ["id"] => string(24) "5b7d6dad7bf162571c0063f4"
  }
  [3] => array(4) {
    ["user_id"] => int(1222)
    ["name"] => string(7) "2344wew"
    ["username"] => string(6) "33r54r"
    ["id"] => string(24) "5b7d6dc77bf162571c0063f5"
  }
  [4] => array(4) {
    ["user_id"] => int(23222)
    ["name"] => string(7) "2344wew"
    ["username"] => string(6) "33r54r"
    ["id"] => string(24) "5b7d7ba57bf1624dc0001651"
  }
}