开发文档 v2.x

表模型(Table)

更新时间:2024年3月11日 15:46 浏览:439

表模型是对数据库表的封装,实现了一些针对表级的操作

表模型通过 Be 工厂获取,Be 工厂中的方法定义:

 public static function getTable(string $name, string $db = 'master'): \Be\Db\Table

参数:

  • string $name - 表名
  • string $db - 库名

返回值:

  • \Be\Db\Table - 表模型

 

表模型产生的 SQL 操作基本都使用了 SQL模板编译和占位符,可以有效防止注入和保障数据安全。本章和 SQL 相关的文档中不再详细标注 SQL编译细节

 

代码示例:

// 表名: user
$table = Be::getTable('user');

// 可指定库名(slave 库下的表 user)
$table = Be::getTable('user', 'slave');

// 查询条件
$table->where('age', 18);

// 获取结果
$articles = $table->getObjects();

// 也可连续操作
$articles = Be::getTable('user')
    ->where('age', 18)
    ->getObjects();

 

导航