



























报错信息
• 请求路由:/student/v2/users/info
• 响应错误: Indirect modification of overloaded element of app\common\model\StudentCacheModel has no effect
这个报错:
Indirect modification of overloaded element of app\common\model\StudentCacheModel has no effect
是 PHP 的一个经典 Notice(有时也会被提升为 Warning 或 Error,取决于错误报告级别),几乎只出现在 ThinkPHP 项目中(因为你用了 app\common\model\ 这种标准的 TP 命名空间)。
你拿到的是通过 __get() 魔法方法返回的临时值(通常是一个数组),然后你试图直接修改这个临时数组,PHP 不允许这样操作,所以报错,而且修改根本不会生效。
最常见的几种写法都会触发这个错误:
// 情况1:最常见
$student = StudentCacheModel::get($id);
$student['some_field'][] = 'new value'; // ← 报错
// 情况2:多级数组修改
$student['config']['level'] = 10; // ← 报错
// 情况3:foreach 里直接改
foreach ($student['tags'] as &$tag) { ... } // ← 也可能报
ThinkPHP 的模型(继承自 think\Model)在你用 $model['field'] 这种数组方式访问时,实际上是通过 __get() 魔法方法返回的:
最推荐:先取出来,改完再赋值回去(最清晰、安全)
$student = StudentCacheModel::get($id);
// 取出
$data = $student['some_field']; // 或 $student->some_field
$data[] = 'new value';
// 写回去
$student['some_field'] = $data; // 或 $student->some_field = $data
// 或者直接用属性方式(如果允许)
$student->some_field = $data;
$student->save(); // 别忘了保存
正确代码
$gainInfo = $this->model->where([
'gid' => $this->gid,
'uid' => $this->uid,
])->findOrEmpty();
if ($gainInfo->isEmpty()) {
$where = [];
$where[] = ['course_id','=',$this->courseId];
$where[] = ['type','=',self::$cacheType];
$where[] = ['create_user','=',$this->uid];
$cacheInfo = StudentCacheModel::where($where)->find();
if ($cacheInfo) {
$result['content'] = json_decode($cacheInfo['content'],true);
if (!empty($result['content']['goods_img1']) && is_string($result['content']['goods_img1'])) {
$result['content']['goods_img1'] = string_to_array($result['content']['goods_img1']);
}
if (!empty($result['content']['live_img1']) && is_string($result['content']['live_img1'])) {
$result['content']['live_img1'] = string_to_array($result['content']['live_img1']);
}
if (!empty($result['content']['face_img1']) && is_string($result['content']['face_img1'])) {
$result['content']['face_img1'] = string_to_array($result['content']['face_img1']);
}
$result['is_cache'] = 1;
return $result;
} else {
return $this->setError(false, '不存在');
}
}
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。