Download the PHP package watsonhaw/think-orm without Composer

On this page you can find all versions of the php package watsonhaw/think-orm. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package think-orm

ThinkORM

基于PHP8.0+ 和PDO实现的ORM,支持多数据库,3.0版本主要特性包括:

安装

composer require watsonhaw/think-orm

文档

详细参考 ThinkORM开发指南

模型事件

模型事件允许在数据写入、更新、删除的前后自动执行自定义逻辑。框架在 ModelEvent trait 中提供了以下事件方法,子类可直接重写:

方法 触发时机 返回值说明
onAfterRead 读取数据后 -
onBeforeInsert 插入前 返回 false 可阻止写入
onAfterInsert 插入后 -
onBeforeUpdate 更新前 返回 false 可阻止更新
onAfterUpdate 更新后 -
onBeforeWrite 写入前(insert/update 通用) 返回 false 可阻止写入
onAfterWrite 写入后(insert/update 通用) -
onBeforeDelete 删除前 返回 false 可阻止删除
onAfterDelete 删除后 -
onBeforeRestore 恢复前 -
onAfterRestore 恢复后 -

Before 与 After 的区别

Before 事件:在数据库操作之前触发,此时修改模型数据会随本次操作一起写入,无需调用 save()。

After 事件:在数据库操作之后触发,此时修改模型数据不会自动持久化,如需保存需显式调用 save()。

注意事项

  1. 不要在 Before 事件中调用 save():会导致无限递归(save() → Before* → save())。
  2. 避免在 onAfterWrite 中调用 save():onAfterWrite 在 insert/update 后都会触发,调用 save() 会再次触发 onAfterWrite,造成递归。
  3. 只有通过模型方法(save()、create()、update()、destroy() 等)写入才会触发事件,直接使用 Db 查询构造器写入不会触发模型事件。

乐观锁使用指南

乐观锁用于处理并发场景下的数据更新冲突问题。其原理是通过在数据表中增加一个版本号字段, 在更新数据时校验版本号是否一致,如果不一致则抛出异常,表示数据已被其他进程修改。

数据库准备

首先需要在数据表中添加版本号字段(字段名可自定义,默认为 lock_version):

模型中使用乐观锁

在模型类中引入 OptimLock trait:

基本使用

创建记录

创建新记录时,乐观锁版本号会自动初始化为 0:

更新记录

每次成功更新记录后,版本号会自动 +1:

处理并发冲突

当两个进程同时读取同一条记录并尝试更新时,先更新成功的进程会使版本号 +1, 后更新的进程会因为版本号不匹配而抛出 OptimLockException 异常:

冲突重试机制

捕获异常后,可以重新读取数据并重试更新操作:

删除操作的乐观锁

删除记录时同样会进行乐观锁检查,如果版本号不匹配则抛出异常:

强制操作(跳过乐观锁检查)

在某些特殊场景下,可以强制跳过乐观锁检查:

自定义版本号字段

如果不想使用默认的 lock_version 字段,可以在模型中自定义:

注意事项

  1. 先读取再更新:使用乐观锁时,必须先通过 find() / select() 等方法从数据库读取数据, 然后再调用 save() 更新。如果直接使用静态 update() 方法而没有设置正确的版本号, 可能无法正确触发乐观锁检查。

  2. 批量更新:乐观锁仅适用于单条记录的 save() 和 delete() 操作。 使用查询构造器执行的批量更新(如 User::where('status', 1)->update(...)) 不会自动触发乐观锁机制。

  3. 事务环境:乐观锁与数据库事务兼容。如果在事务中更新失败抛出异常, 事务会自动回滚(前提是更新操作在事务内)。

  4. 高并发场景:在高并发写入场景下,建议配合合理的重试机制使用, 同时考虑是否需要使用悲观锁(Db::lock(true))等其他方案。

参与开发

单元测试编写

创建创建一个名为 UserInfo 的迁移文件(以测试单元为单位来创建迁移)

迁移命令

下面相关命令都是 mysql 与 pgsql 同时执行,如果环境不完整可以通过 phinx 手动执行独立的迁移命令。

执行迁移(mysql、pgsql)

重建,先回滚在迁移(mysql、pgsql)

回滚迁移(mysql、pgsql)

迁移状态(mysql、pgsql)

环境问题

  1. 如果提示 phinx 不存在,尝试手动执行composer bin phinx install安装。

All versions of think-orm with dependencies

PHP Build Version
Package Version
Requires php Version >=8.1
ext-json Version *
ext-pdo Version *
psr/simple-cache Version ^1.0|^2.0
psr/log Version ^1.0|^2.0|^3.0
topthink/think-helper Version ^3.1
ext-openssl Version *
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package watsonhaw/think-orm contains the following files

Loading the files please wait ...