albertwill/tdengine-orm

Maintainers

👁 AlbertWill

Package info

github.com/AlbertWill/tdengine-orm

pkg:composer/albertwill/tdengine-orm

Statistics

Installs: 86

Dependents: 0

Suggesters: 0

Stars: 0

v1.1.6 2025-11-25 11:43 UTC

Suggests

None

Provides

None

Conflicts

None

Replaces

None

MIT 315563cae2410fce9ab4498d2733f2ff65553218

This package is auto-updated.

Last update: 2026-06-25 14:42:11 UTC


README

👁 Latest Version
👁 GitHub Workflow Status (branch)
👁 Php Version
👁 License

简介

基于 tdengine-restful-connectorphp-tdengine 开发的 TDEngine ORM。

支持创建超级表、创建表、批量插入数据。

此项目支持在 PHP >= 7.1 的项目中使用。

支持在 ThinkPHP、Laravel、Swooleimi 等项目中使用

在 Swoole 环境中支持协程化,不会阻塞!

安装

composer require albertwill/tdengine-orm

使用

使用连接管理器:

// 增加名称为 test 的连接配置
\Yurun\TDEngine\TDEngineManager::setClientConfig('test', new \Yurun\TDEngine\ClientConfig([
 // 'host' => '127.0.0.1',
 // 'hostName' => '',
 // 'port' => 6041,
 // 'user' => 'root',
 // 'password' => 'taosdata',
 // 'ssl' => false,
 // 'timestampFormat' => \Yurun\TDEngine\Constants\TimeStampFormat::LOCAL_STRING,
 // 'keepAlive' => true,
]));
// 设置默认数据库为test
\Yurun\TDEngine\TDEngineManager::setDefaultClientName('test');
// 获取客户端对象(\Yurun\TDEngine\Client)
$client = \Yurun\TDEngine\TDEngineManager::getClient();
// 不设置连接驱动时,会判断安装扩展优先使用扩展
// 设置连接驱动为 RESTful
\Yurun\TDEngine\Orm\TDEngineOrm::setClientHandler(new \Yurun\TDEngine\Orm\ClientHandler\Restful\Handler());
// 设置连接驱动为 PHP 扩展
\Yurun\TDEngine\Orm\TDEngineOrm::setClientHandler(new \Yurun\TDEngine\Orm\ClientHandler\Extension\Handler());

定义模型:

<?php

declare(strict_types=1);

namespace Yurun\TDEngine\Orm\Test\Model;

use Yurun\TDEngine\Orm\Annotation\Field;
use Yurun\TDEngine\Orm\Annotation\Table;
use Yurun\TDEngine\Orm\Annotation\Tag;
use Yurun\TDEngine\Orm\BaseModel;
use Yurun\TDEngine\Orm\Enum\DataType;

/**
 * @Table(name="device_log", database="device")
 */
class DeviceLogModel extends BaseModel
{
 /**
 * @Field(type=DataType::TIMESTAMP)
 *
 * @var int
 */
 public $time;

 /**
 * @Tag(type=DataType::NCHAR, length=32, name="device_id")
 *
 * @var string
 */
 public $deviceId;

 /**
 * @Field(type=DataType::FLOAT)
 *
 * @var float
 */
 public $voltage;

 /**
 * @Field(type=DataType::FLOAT, name="electric_current")
 *
 * @var float
 */
 public $electricCurrent;
}

创建超级表:

DeviceLogModel::createSuperTable();

创建超级表的子表:

$table = '表名';
$deviceId = '00000001'; // 这是 TAGS
DeviceLogModel::createSubTable($table, [$deviceId]);

创建普通表:

DeviceLogModel::createTables();

插入数据:

$record = new DeviceLogModel([
 // 初始化模型数据
], '表名');
// $record->xxx = xxx; // 设置一些字段值
$record->insert();

批量插入数据:

$record1 = new DeviceLogModel([
 // 初始化模型数据
], '表名1');
$record2 = new DeviceLogModel([
 // 初始化模型数据
], '表名2');
DeviceLogModel::batchInsert([$record1, $record2]);