PHP code example of yurunsoft / tdengine-orm
1. Go to this page and download the library: Download yurunsoft/tdengine-orm 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/ */
yurunsoft / tdengine-orm example snippets
// 增加名称为 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());
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::createTable($table, [$deviceId]);
$record = new DeviceLogModel([
// 初始化模型数据
], '表名');
// $record->xxx = xxx; // 设置一些字段值
$record->insert();
$record1 = new DeviceLogModel([
// 初始化模型数据
], '表名1');
$record2 = new DeviceLogModel([
// 初始化模型数据
], '表名2');
DeviceLogModel::batchInsert([$record1, $record2]);