PHP code example of tangwei / database-doris

1. Go to this page and download the library: Download tangwei/database-doris library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

tangwei / database-doris example snippets


'doris' => [
    'driver'    => 'doris',
    'host'      => '127.0.0.1',
    'database'  => 'your_database',
    'port'      => 9030,
    'username'  => 'root',
    'password'  => '',
    'charset'   => 'utf8',
    'prefix'    => '',
    'pool'      => [
        'min_connections' => 1,
        'max_connections' => 20,
        'connect_timeout' => 10.0,
        'wait_timeout'    => 8.0,
        'heartbeat'       => -1,
        'max_idle_time'   => 60,
    ],
],

'doris_catalog_mysql' => [
    'driver'    => 'doris_catalog_mysql',
    'host'      => '127.0.0.1',
    'catalog'   => 'mysql',  // Doris 中创建的 MySQL Catalog 名称
    'database'  => 'target_database',
    'port'      => 9030,
    'username'  => 'root',
    'password'  => '',
    'charset'   => 'utf8',
    'prefix'    => '',
    'pool'      => [
        'min_connections' => 1,
        'max_connections' => 20,
        'connect_timeout' => 10.0,
        'wait_timeout'    => 8.0,
        'heartbeat'       => -1,
        'max_idle_time'   => 60,
    ],
],

'doris_catalog_pgsql' => [
    'driver'    => 'doris_catalog_pgsql',
    'host'      => '127.0.0.1',
    'catalog'   => 'postgresql',  // Doris 中创建的 PG Catalog 名称
    'database'  => 'public',
    'port'      => 9030,
    'username'  => 'postgres',
    'password'  => 'password',
    'charset'   => 'utf8',
    'prefix'    => '',
    'pool'      => [
        'min_connections' => 1,
        'max_connections' => 20,
        'connect_timeout' => 10.0,
        'wait_timeout'    => 8.0,
        'heartbeat'       => -1,
        'max_idle_time'   => 60,
    ],
],

'doris_catalog_sqlsrv' => [
    'driver'    => 'doris_catalog_sqlsrv',
    'host'      => '127.0.0.1',
    'catalog'   => 'sqlserver',  // Doris 中创建的 SQL Server Catalog 名称
    'database'  => 'dbo',
    'port'      => 9030,
    'username'  => 'sa',
    'password'  => 'password',
    'charset'   => 'utf8',
    'prefix'    => '',
    'pool'      => [
        'min_connections' => 1,
        'max_connections' => 20,
        'connect_timeout' => 10.0,
        'wait_timeout'    => 8.0,
        'heartbeat'       => -1,
        'max_idle_time'   => 60,
    ],
],

'doris_es' => [
    'driver'    => 'doris_catalog',
    'host'      => '127.0.0.1',
    'catalog'   => 'es',              // Doris 中创建的 ES Catalog 名称
    'database'  => 'default_db',
    'port'      => 9030,
    'username'  => 'root',
    'password'  => '',
    'charset'   => 'utf8',
    'prefix'    => '',
    'pool'      => [
        'min_connections' => 1,
        'max_connections' => 20,
        'connect_timeout' => 10.0,
        'wait_timeout'    => 8.0,
        'heartbeat'       => -1,
        'max_idle_time'   => 60,
    ],
],

    'doris_catalog_oracle' => [
    'driver'    => 'doris_catalog_oracle',
    'host'      => '127.0.0.1',
    'catalog'   => 'oracle',  // Doris 中创建的 oracle Catalog 名称
    'database'  => 'dbo',
    'port'      => 9030,
    'username'  => 'sa',
    'password'  => 'password',
    'charset'   => 'utf8',
    'prefix'    => '',
    'pool'      => [
        'min_connections' => 1,
        'max_connections' => 20,
        'connect_timeout' => 10.0,
        'wait_timeout'    => 8.0,
        'heartbeat'       => -1,
        'max_idle_time'   => 60,
    ],
],



declare(strict_types=1);

namespace App\Model;

use Hyperf\DbConnection\Model\Model;

class GoodsDoris extends Model
{
    protected ?string $table = 'goods';
    
    protected ?string $connection = 'doris';
}



declare(strict_types=1);

namespace App\Model;

use Hyperf\DbConnection\Model\Model;

class GoodsDorisCatalog extends Model
{
    protected ?string $table = 'goods';
    
    protected ?string $connection = 'doris_catalog_mysql';
}

use App\Model\GoodsDorisCatalog;
use Hyperf\DbConnection\Db;

// 查询
$goods = GoodsDorisCatalog::query()->where('id', 1)->first();
$list = GoodsDorisCatalog::query()->where('status', 1)->get();
$count = GoodsDorisCatalog::query()->count();

// 插入
GoodsDorisCatalog::query()->insert([
    'goods_id' => 123,
    'goods_code' => 'GC001',
    'goods_name' => '测试商品',
]);

// 更新
GoodsDorisCatalog::query()
    ->where('id', 1)
    ->update(['goods_name' => '新商品名称']);

// 删除
GoodsDorisCatalog::query()->where('id', 1)->delete();

// MySQL Catalog
$data = Db::connection('doris_catalog_mysql')
    ->table('goods')
    ->where('id', '>', 100)
    ->orderBy('created_at', 'desc')
    ->limit(10)
    ->get();

// PostgreSQL Catalog
$user = Db::connection('doris_catalog_pgsql')
    ->table('user')
    ->where('number', '10')
    ->first();

// SQL Server Catalog
$records = Db::connection('doris_catalog_sqlsrv')
    ->table('spzl')
    ->limit(5)
    ->get();
    
// Oracle Catalog    
$records = Db::connection('doris_catalog_oracle')
    ->table('oracle')
    ->limit(5)
    ->get();

// Elasticsearch Catalog
$count = Db::connection('doris_es')
    ->table('order')
    ->where(function ($query) {
        $query->where('status', 1)
              ->orWhere('createdTime', '>', 1750032311);
    })
    ->count();

// 子查询
$goods = GoodsDorisCatalog::query()
    ->whereHas('orders', function ($query) {
        $query->where('status', 'paid');
    })
    ->get();

// 关联查询
$goods = GoodsDorisCatalog::query()
    ->with(['category', 'supplier'])
    ->where('price', '>', 100)
    ->get();

// 聚合查询
$result = GoodsDorisCatalog::query()
    ->selectRaw('category_id, COUNT(*) as count, SUM(price) as total')
    ->groupBy('category_id')
    ->havingRaw('SUM(price) > 1000')
    ->get();

'commands' => [
    'gen:model' => [
        'path'          => 'app/Model',
        'force_casts'   => true,
        'inheritance'   => 'Model',
        'with_comments' => true,      // 生成字段注释
        'property_case' => 1,         // 属性命名风格
        'uses'          => '',
    ],
],

// 安全,组件会自动转义
GoodsDorisCatalog::query()->insert([
    'goods_code' => "12\\'3",  // 自动处理引号和反斜杠
]);
bash
# 生成 Doris 模型
php bin/hyperf.php gen:model doris --table=goods

# 生成指定连接的模型
php bin/hyperf.php gen:model --pool doris_catalog_mysql goods