定制 kiss-php/tables 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

kiss-php/tables

Composer 安装命令:

composer require kiss-php/tables

包简介

Kiss ORM for php.

README 文档

README

Tables is a small PHP ORM-like library that maps simple .tbl files to PDO rows. It is designed for quick projects where table definitions live beside the code and the library can create or repair the schema when a table or column is missing.

Install

composer require kiss-php/tables

Or, inside this repository:

composer dump-autoload

Configuration

Connections are read from $_ENV. The default connection uses the DB_ prefix:

$_ENV['DB_TYPE'] = 'sqlite';
$_ENV['DB_PATH'] = __DIR__ . '/db/app.sqlite';

MySQL is also supported:

$_ENV['DB_TYPE'] = 'mysql';
$_ENV['DB_HOST'] = 'localhost';
$_ENV['DB_USER'] = 'root';
$_ENV['DB_PASS'] = '';
$_ENV['DB_DTBS'] = 'app';
$_ENV['DB_PORT'] = '3306';

Named connections use DB_<FLAG>_, for example DB_LITLE_TYPE and DB_LITLE_PATH, then pass the flag to TablesManager or Row methods.

Table Definitions

Create .tbl files in a folder, one file per table:

# Tables/User.tbl
user[varchar(90)]:unique
mail[varchar(255)]
active[boolean](true)
phone[varchar(25)]:null
profile@UserProfile.user_id

Supported syntax:

  • field[type]
  • field[type](default)
  • field[type]:null:unique:unsigned:zero:binary:increment
  • field[type]$OtherTable.id for a foreign key stored on the current table
  • field@OneTable.current_id for a virtual one-to-one lookup
  • field@ManyTable.current_id for a virtual one-to-many lookup
  • legacy_id=OtherTable.id for older relation definitions

Every table gets an auto-increment id column automatically.

Usage

require 'vendor/autoload.php';

use Kiss\Tables\TablesManager;

TablesManager::setFolder(__DIR__ . '/Tables');
TablesManager::ensureSchema();

$user = TablesManager::new('User');
$user->setUser('ada');
$user->setMail('ada@example.test');
$user->persist();

$found = TablesManager::getOne('User', ['user' => 'ada']);
$found->setMail('ada@lovelace.test');
$found->persist();

Rows expose dynamic getters and setters. CamelCase method names are converted to snake_case database columns:

$user->setMailVerified(true); // mail_verified
$user->getMailVerified();

Schema Repair

persist() and get() recover from missing tables by creating the table from its .tbl definition. persist() can also add missing columns and retry the operation. This auto-repair behavior is enabled by default because it is the core workflow of the library.

You can disable it for stricter environments:

TablesManager::setAutoRepair(false);

For explicit setup, call:

TablesManager::ensureSchema();

Rollback

Rows changed through persist() or delete() are tracked. Calling:

TablesManager::rollback();

restores updated/deleted rows or removes rows inserted during the current process.

History Logs

Tables keeps a small in-memory history. It stores SQL parameters as they are provided. This keeps the library small and predictable, but it also means you should not print or export history in production unless you are comfortable with the values it contains.

You can route logs wherever you want with an optional callback:

use Kiss\Tables\History;

History::callback(function (array $entry) {
    // Send to Monolog, a PSR-3 logger, stdout, a file, etc.
    // $entry contains: type, message, context.
    // Redact or drop sensitive values here if your app needs it.
});

Database exceptions are logged as sanitized error events with the exception class and code, not the raw database error message.

Security and Operational Notes

Tables is a lightweight library, not a full application security layer. Keep these points in mind when using it:

  • Treat .tbl files as trusted code. They drive table names, column names, column types, defaults and relations. Do not load table definition folders that can be edited by end users.
  • Auto-repair is enabled by default and may run CREATE TABLE or ALTER TABLE during normal reads/writes. This is useful for KISS-style development, but it means the database user needs DDL permissions. In stricter deployments, run TablesManager::ensureSchema() during deploy and then call TablesManager::setAutoRepair(false).
  • History stores SQL parameters as provided. Do not call History::printAll() or export History::getAll() in production unless your application has already removed or filtered sensitive values.
  • If you use History::callback(), the callback belongs to your application: redact sensitive data there, send logs to your preferred logger, and avoid throwing from the callback unless you intentionally want logging failures to interrupt database operations.
  • TablesManager::rollback() is an in-memory convenience helper, not a database transaction. It is useful for simple undo flows in the current PHP process, but it does not provide ACID guarantees or protect concurrent writes.
  • Database credentials should come from environment/configuration management. Do not hardcode real credentials in examples, scripts or committed files.

Smoke Test

Run the SQLite smoke test:

php tests/smoke.php

kiss-php/tables 适用场景与选型建议

kiss-php/tables 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 13 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 01 月 03 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 kiss-php/tables 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 kiss-php/tables 我们能提供哪些服务?
定制开发 / 二次开发

基于 kiss-php/tables 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 13
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 10
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-01-03