执行宠物技能处理命令 php artisan pet:process-active-skills --sync 时出现错误:
TypeError: Cannot access offset of type string on string at /data/wwwroot/nusuus/kknongchang/kku_laravel/app/Module/Pet/Logic/PetAutoSkillLogic.php:690
错误发生在 PetAutoSkillLogic.php 的 recordSkillStatistics 方法中。问题原因:
PetActiveSkill 模型的 config 字段虽然定义了 'config' => 'array' 的castjson_encode() 存储配置,导致数据库中存储的是JSON字符串$activeSkill->config 返回字符串而不是数组Cannot access offset of type string on string 错误在 recordSkillStatistics 方法中添加类型检查和转换逻辑,确保 config 字段始终为数组类型:
protected function recordSkillStatistics(PetActiveSkill $activeSkill, string $actionType, array $statistics): void
{
$config = $activeSkill->config;
// 确保config是数组类型
if (!is_array($config)) {
// 如果是字符串,尝试解析JSON
if (is_string($config)) {
$config = json_decode($config, true);
if (json_last_error() !== JSON_ERROR_NONE) {
$config = [];
}
} else {
$config = [];
}
}
// 其余逻辑保持不变...
}
app/Module/Pet/Logic/PetAutoSkillLogic.php - 修复 recordSkillStatistics 方法的类型安全问题修复后重新执行命令:
php artisan pet:process-active-skills --sync
执行成功,输出:
开始处理宠物激活技能...
使用同步模式处理...
宠物激活技能处理完成(同步模式)
日志显示正常处理了1个激活技能,没有任何错误。
updateLastCheckTime、getLastCheckTime 等)一致的类型安全处理逻辑修复宠物技能处理中config字段类型错误问题
- 在recordSkillStatistics方法中添加config字段类型检查
- 确保config字段始终为数组类型,避免字符串类型导致的TypeError
- 修复Cannot access offset of type string on string错误
- 保持与模型中其他方法一致的类型安全处理逻辑