Skip to content

Commit d8a05af

Browse files
houaiaiclaude
andcommitted
fix: use setOption() method for think-orm 4.0 compatibility
- Use setOption() instead of direct property assignment - Implement initialize() method for model configuration - Prevent model data pollution as per think-orm 4.0 upgrade guide - Fix table name fallback when rules_name is null According to think-orm 4.0 official documentation: "如果需要在模型内获取或设置模型属性的话改为使用模型的getOption或setOption方法" This resolves: - WeakMap initialization errors - array_search type errors - Table name configuration issues Reference: https://doc.thinkphp.cn/@think-orm/v4_0/upgrade.html Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent c81e0d0 commit d8a05af

1 file changed

Lines changed: 14 additions & 5 deletions

File tree

src/Model/RuleModel.php

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,16 +45,25 @@ public function __construct(array $data = [], ?string $driver = null)
4545
{
4646
$this->driver = $driver;
4747

48-
// 必须先调用父类构造函数,确保 WeakMap 初始化(think-orm 4.0+)
48+
// 调用父类构造函数,确保 WeakMap 初始化(think-orm 4.0+)
4949
parent::__construct($data);
50+
}
51+
52+
/**
53+
* 模型初始化
54+
* @return void
55+
*/
56+
protected function initialize(): void
57+
{
58+
parent::initialize();
5059

51-
// 再设置其他属性,避免触发 __set() 时 WeakMap 未初始化
52-
$this->connection = $this->config('database.connection') ?: '';
53-
$this->table = $this->config('database.rules_table');
60+
// 使用 setOption 方法设置模型属性(think-orm 4.0 推荐方式)
61+
$this->setOption('connection', $this->config('database.connection') ?: '');
62+
$this->setOption('table', $this->config('database.rules_table'));
5463

5564
// 如果 rules_name 为 null,使用 rules_table 作为表名
5665
$rulesName = $this->config('database.rules_name');
57-
$this->name = $rulesName ?: $this->config('database.rules_table');
66+
$this->setOption('name', $rulesName ?: $this->config('database.rules_table'));
5867
}
5968

6069
/**

0 commit comments

Comments
 (0)