[BUG] PHP 8.5: Model::toArray() throws deprecation when UPDATED_AT is null

Ouverte Adaptée aux débutants
#7,836 0 commentaires 0 réactions 0 personnes assignées Voir sur GitHub

Personne n'a encore pris cette issue.

Évaluation

Difficulté
2/5
Temps estimé
1-3 heures
Accessibilité débutants
78/100
Type d'issue
Bug
Clarté
Clairement spécifiée
Activité
Active
Stack technique
php
Domaine
backend, testing

Piste de recherche

Lisez src/Model/Concerns/HasAttributes.php, en particulier getDates() et addDateAttributesToArray(), puis recherchez les tests de sérialisation du modèle. Reproduisez le problème avec un modèle dont UPDATED_AT est null et dont CREATED_AT est défini. Le travail est terminé lorsque toArray() conserve l’heure de création et renvoie le résultat sans dépréciation lorsque l’horodatage de mise à jour est désactivé.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Description

bug

Execute the command and paste the result below.

Command: uname -a && php -v && composer info | grep hyperf && php --ri swoole

Linux 09fabbe4eaf7 7.0.14-linuxkit #1 SMP PREEMPT_DYNAMIC Fri Oct 30 23:00:25 UTC 2026 x86_64 Linux
PHP 8.5.12 (cli) (built: Oct 21 2026 22:49:34) (NTS)
Copyright (c) The PHP Group
Built by https://github.com/docker-library/php
Zend Engine v4.5.12, Copyright (c) Zend Technologies
    with Zend OPcache v8.5.12, Copyright (c), by Zend Technologies
hyperf-extension/jwt               3.1.23.0           The Hyperf JWT package.
hyperf/amqp                        3.2.5              A amqplib for hyperf.
hyperf/async-queue                 3.2.2              A async queue compone...
hyperf/cache                       3.2.0              A cache component for...
hyperf/code-parser                 3.2.0              A code parser compone...
hyperf/codec                       3.2.0              A codec component for...
hyperf/collection                  3.2.5              Hyperf Collection pac...
hyperf/command                     3.2.0              Command for hyperf
hyperf/conditionable               3.2.0              Hyperf Macroable pack...
hyperf/config                      3.2.0              An independent compon...
hyperf/constants                   3.2.0              A constants component...
hyperf/context                     3.2.0              A coroutine/applicati...
hyperf/contract                    3.2.0              The contracts of Hyperf.
hyperf/coordinator                 3.2.1.1            Hyperf Coordinator
hyperf/coroutine                   3.2.1              Hyperf Coroutine
hyperf/crontab                     3.2.0              A crontab component f...
hyperf/database                    3.2.5              A flexible database l...
hyperf/database-pgsql              3.2.2              A pgsql handler for h...
hyperf/db-connection               3.2.0              A hyperf db connectio...
hyperf/devtool                     3.2.0              A Devtool for Hyperf.
hyperf/di                          3.2.2              A DI for Hyperf.
hyperf/dispatcher                  3.2.0              A HTTP Server for Hyp...
hyperf/elasticsearch               3.2.0              Elasticsearch client ...
hyperf/engine                      2.15.0             Coroutine engine prov...
hyperf/engine-contract             1.14.0             Contract for Coroutin...
hyperf/event                       3.2.0              an event manager that...
hyperf/exception-handler           3.2.0              Exception handler for...
hyperf/filesystem                  3.2.0              flysystem integration...
hyperf/flysystem-oss               1.4.0
hyperf/framework                   3.2.0              A coroutine framework...
hyperf/gotask                      3.0.1              A replacement for Swo...
hyperf/grpc                        3.2.0              A GRPC basic library ...
hyperf/grpc-client                 3.2.0              A GRPC Client for Hyp...
hyperf/grpc-server                 3.2.0              A GRPC Server for Hyp...
hyperf/guzzle                      3.2.0              Swoole coroutine hand...
hyperf/http-message                3.2.0              microservice framewor...
hyperf/http-server                 3.2.0              A HTTP Server for Hyp...
hyperf/json-rpc                    3.2.0              A JSON RPC component ...
hyperf/kafka                       3.2.0              A kafka client for Hy...
hyperf/load-balancer               3.2.0              A load balancer libra...
hyperf/logger                      3.2.4              A logger component fo...
hyperf/macroable                   3.2.0              Hyperf Macroable pack...
hyperf/memory                      3.2.5              An independent compon...
hyperf/model-cache                 3.2.3              A model cache compone...
hyperf/model-listener              3.2.0              A model listener for ...
hyperf/pipeline                    3.2.0              Hyperf Macroable pack...
hyperf/pool                        3.2.1              An independent univer...
hyperf/process                     3.2.0              A process component f...
hyperf/rate-limit                  3.2.0              A rate limiter implem...
hyperf/redis                       3.2.0              A redis component for...
hyperf/retry                       3.2.0              A retry component for...
hyperf/rpc                         3.2.0              A rpc basic library f...
hyperf/rpc-client                  3.2.0              An abstract rpc serve...
hyperf/rpc-server                  3.2.0              An abstract rpc serve...
hyperf/serializer                  3.2.0              A serializer componen...
hyperf/server                      3.2.0              A base server library...
hyperf/signal                      3.2.0              A signal library for ...
hyperf/snowflake                   3.2.0              A snowflake library
hyperf/stdlib                      3.2.0              A stdlib component fo...
hyperf/stringable                  3.2.5              Hyperf Stringable pac...
hyperf/support                     3.2.4              A support component f...
hyperf/tappable                    3.2.0              Hyperf Macroable pack...
hyperf/testing                     3.2.0              Testing for hyperf
hyperf/token-bucket                2.0.0              Implementation of the...
hyperf/tracer                      3.2.4              A open tracing system...
hyperf/translation                 3.2.0              An independent transl...
hyperf/validation                  3.2.5              hyperf validation
hyperf/websocket-server            3.2.1              A websocket server li...

swoole

Swoole => enabled
Author => Swoole Team <[email protected]>
Version => 6.2.3
Built => Nov  4 2026 15:59:43
host byte order => little endian
coroutine => enabled with boost asm context
epoll => enabled
eventfd => enabled
signalfd => enabled
cpu_affinity => enabled
spinlock => enabled
rwlock => enabled
sockets => enabled
openssl => OpenSSL 3.5.9 29 Sep 2026
dtls => enabled
http2 => enabled
json => enabled
curl-native => enabled
curl-version => 8.23.0
c-ares => 1.34.9
zlib => 1.3.2
brotli => E16785409/D16785409
mutex_timedlock => enabled
pthread_barrier => enabled
futex => enabled
mysqlnd => enabled
postgresql(libpq) version => 18.6
coroutine_pgsql => enabled

Directive => Local Value => Master Value
swoole.enable_library => On => On
swoole.enable_fiber_mock => Off => Off
swoole.enable_preemptive_scheduler => Off => Off
swoole.display_errors => On => On
swoole.use_shortname => Off => Off
swoole.socket_buffer_size => 8388608 => 8388608
swoole.blocking_detection => Off => Off
swoole.blocking_threshold => 100000 => 100000
swoole.profile => Off => Off
swoole.leak_detection => Off => Off
Description:

在 PHP 8.5.12、hyperf/database v3.2.5 环境下,模型通过
UPDATED_AT = null 禁用更新时间字段后,调用 toArray() 会触发:

Using null as an array offset is deprecated, use an empty string instead

在 Hyperf 将弃用提示转换为 ErrorException 的环境中,该问题会中断正常查询响应。

定位到 Hyperf\Database\Model\Concerns\HasAttributes:

  1. getDates() 将 CREATED_AT 和 UPDATED_AT 加入日期字段列表,没有过滤 null。
  2. addDateAttributesToArray() 遍历该列表并执行 isset($attributes[$key])。
  3. 当 $key === null 时,触发 PHP 8.5 弃用提示。

预期行为:禁用更新时间字段的模型可以正常序列化,保留创建时间,不触发弃用提示。

建议在生成日期字段列表时过滤表示禁用时间列的 null,并增加空时间列常量的序列化回归测试。

Steps To Reproduce:
  1. 使用 PHP 8.5.12 和 hyperf/database v3.2.5。
  2. 在项目根目录创建 reproduce.php,内容如下。
  3. 执行 php reproduce.php。该复现不需要数据库连接,也不依赖业务模型或 HTTP 请求。
<?php

require __DIR__ . '/vendor/autoload.php';

error_reporting(E_ALL);

// 模拟框架将弃用提示转换为异常的行为。
set_error_handler(static function ($level, $message, $file, $line) {
    throw new ErrorException($message, 0, $level, $file, $line);
});

$model = new class extends \Hyperf\Database\Model\Model {
    public const CREATED_AT = 'create_time';
    public const UPDATED_AT = null;

    protected ?string $dateFormat = 'Y-m-d H:i:s';
};

$model->setRawAttributes([
    'id' => 1,
    'create_time' => '2026-11-12 12:00:00',
]);

var_export($model->getDates());
var_export($model->toArray());

实际结果:

array (
  0 => 'create_time',
  1 => NULL,
)

ErrorException: Using null as an array offset is deprecated,
use an empty string instead

vendor/hyperf/database/src/Model/Concerns/HasAttributes.php:731
vendor/hyperf/database/src/Model/Concerns/HasAttributes.php:122
vendor/hyperf/database/src/Model/Model.php:879

预期 toArray() 返回:

[
    'id' => 1,
    'create_time' => '2026-11-12 12:00:00',
]
Langage dominant
PHP
Étoiles
6.9k
Forks
1.3k
Merge moyen
1 h 17 min
PR mergées (30 j)
3

Préparer son environnement

Par où commencer

  1. Lisez l'issue en entier, puis le guide de contribution du projet.
  2. Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
  3. Forkez le dépôt et travaillez sur une branche.
  4. Ouvrez une pull request qui référence le numéro de l'issue.

Autres issues de hyperf/hyperf

Toutes les issues de hyperf/hyperf

Issues similaires

Plus d'issues PHP

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.