ブログ記事一覧が1件しか表示されない不具合を修正 - #4503
Merged
Merged
Conversation
BcArrayHelper::first() が Query を受け取ると toArray() で実行しており、 呼び出し元が反復中の ResultSet を消費してしまっていた。 CakePHP 5.2 の ResultSet はイテレータを共有するため、foreach の1件目で first() が呼ばれた時点で残りが読み切られ、2件目以降が描画されない。 Query の反復は 0 起点の連番キーとなるため、実行せずキーの比較のみで判定する。 last() の count() は件数取得用のクエリを別途発行し反復に影響しないため変更しない。 テストは toArray() の戻り値を1起点のキー配列とするモックを前提としており、 実際の反復キー(0起点)と異なっていたため本件を検出できていなかった。 Query を実行しないことを検証する形に改めた。 Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Contributor
There was a problem hiding this comment.
Pull request overview
ブログ記事一覧テンプレートで Query を foreach 反復中に BcArrayHelper::first() を呼ぶと、ResultSet の共有イテレータが消費されて2件目以降が描画されない不具合(#4501)を、BcArrayHelper 側で解消するPRです。
Changes:
BcArrayHelper::first()がQueryを受け取った場合にtoArray()で実行せず、反復キー(0起点)だけで先頭判定するよう変更BcArrayHelper::last()のQuery->count()が反復中のResultSetを消費しない旨のコメントを追加BcArrayHelperTest::testFirstWithQuery()を「toArray()を呼ばないこと」と「0起点判定」を検証する形に修正
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| plugins/baser-core/src/View/Helper/BcArrayHelper.php | Query を反復中に消費しないよう first() の実装を変更し、ブログ一覧の表示欠けを防止 |
| plugins/baser-core/tests/TestCase/View/Helper/BcArrayHelperTest.php | Query に対する first() の期待動作(未実行・0起点判定)へテストを更新 |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
fix #4501
原因
BcArrayHelper::first()はQueryを受け取るとtoArray()で実行していました。CakePHP 5.2 の
ResultSetはイテレータを共有するため、テンプレートのforeachが1件目を取り出した直後にfirst()が同じ ResultSet を最後まで読み切ってしまいます。結果としてforeachに戻った時点で残りが消費済みとなり、2件目以降が描画されずループが終了していました。修正
Queryの反復は 0 起点の連番キーとなるため、実行せずキーの比較のみで判定するようにしました。last()のcount()は件数取得用のクエリを別途発行し、反復中の ResultSet を消費しないため変更していません(意図が伝わるようコメントのみ追加)。Issue の修正案との違い
Issue では恒久対応として
BlogHelper::getPosts()での配列化が挙げられていますが、下記の理由からBcArrayHelper側での修正としました。getPosts()の戻り値の型(Query)を変えずに済む。getIndex(): Queryとの一貫性が保てるテストについて
既存の
testFirstWithQueryは、toArray()が 1起点のキー配列を返すモックを前提としていました。実際のQueryの反復は 0 起点であり、モックが実態と異なっていたため本件を検出できていませんでした。実データで反復キーが 0 起点であることを確認したうえで、「
toArray()を呼ばないこと」と「0 起点で判定すること」を検証する形に改めています。修正前の実装に戻すとこのテストが失敗することも確認済みです。動作確認
BcColumn テーマのトップページで、works / news / topics の各一覧が 1件 → 4件(
blogPosts(..., 4)の指定どおり)になることを確認しました。first/lastのクラス付与も正しく機能しています。BcArrayHelperTestは8件すべて成功します。