CakePHP 5.3 で EntityTrait::isEmpty() が deprecatedになりました。
Since 5.3.0: isEmpty() is deprecated. Use hasValue() instead.
公式ドキュメントのマイグレーションガイド(2026年6月時点で日本語版はないので英語版より)には、以下のようにあります。
EntityTrait::isEmpty() is deprecated. Use hasValue() instead.
ただし、「isEmpty を hasValue に単純置換」すると return が逆になるので要注意 です。
「逆」になっていること
vendor の定義(要約):
// Cake\Datasource\EntityTrait
public function isEmpty(string $field): bool // 値が「空なら」true
public function hasValue(string $field): bool // 値が「有るなら」true
つまり:
$entity->isEmpty($f) === !$entity->hasValue($f)
となる真偽が逆となります。
deprecation対応
// Before
if ($entity->isEmpty('title')) {
// title が空のときの処理
}
// After(! を付けること)
if (!$entity->hasValue('title')) {
// title が空のときの処理
}
なぜ deprecated になったのか
議論(PR #18354)で挙がっている理由は次の通りです。
-
冗長(逆向きの同じものが2つある)。
hasValue()はisEmpty()の戻り値を否定しただけで、両方を持つ必要がない。どちらかに一本化しよう、という提案。 -
isEmpty()の挙動が PHP のempty()と違って紛らわしい。CakePHP のisEmpty()/hasValue()は「フィールドに値があるか」を見るもので、PHP ネイティブのempty()(0や'0'も空扱い)とは判定が異なる。名前からempty()を連想すると紛らわしい。 -
isEmpty()はEntityInterfaceに無い。インターフェースの正規メンバーはhasValue()のみで、isEmpty()は trait にだけ存在していた。インターフェース側に揃える形でhasValue()を残した。 - 6.0 クリーンアップの一環(6.0-Ideas wiki )。
「真偽が逆なだけの重複メソッドで、しかも empty() と紛らわしく、インターフェースにも無いので、インターフェース側の hasValue() に一本化した」ということのようです。
補足: Collection::isEmpty() と混同に注意
->isEmpty( という文字列は、Entity の isEmpty($field) だけでなく Cake\Collection\Collection::isEmpty()(引数なし・非推奨ではない) もあります。
$entity->isEmpty('title'); // ← 非推奨(Entity・引数あり)
$collection->isEmpty(); // ← こちらは非推奨ではない
まとめ
-
$entity->isEmpty($f)→!$entity->hasValue($f)。 -
Collection::isEmpty()(引数なし)との混同に注意。 - 6.0 で削除。5.x のうちに、否定の付け外しを確認しながら修正することを推奨。
参考
- 5.3 Migration Guide — https://book.cakephp.org/5/en/appendices/5-3-migration-guide.html
- EntityTrait(
@deprecated 5.3.0 Use hasValue() instead.)— https://github.com/cakephp/cakephp/blob/5.x/src/Datasource/EntityTrait.php - PR #18354 5.next: Deprecate isEmpty() in favor of hasValue() — https://github.com/cakephp/cakephp/pull/18354
- PR #18335 6.x: Drop isEmpty() in favor of hasValue()(廃止理由の議論)— https://github.com/cakephp/cakephp/pull/18335