fix #4220 【ブログ】特定条件でコピーした記事がコピーできない件を修正#4386
Open
kaburk wants to merge 1 commit into
Open
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
ブログ記事のコピー処理において、スラッグ(name)やタイトル(title)の長さ制限・重複に起因してコピーに失敗するケース(#4220)を回避するための調整を行うPRです。
Changes:
- コピー時に
name/titleを_copy付与前提で長さを切り詰め、255文字制限を超えないように修正 nameが重複する場合にサフィックス(_1〜)を付与してユニーク化するロジックを追加PersistenceFailedExceptionの再帰リトライ処理を削除
Comments suppressed due to low confidence (2)
plugins/bc-blog/src/Model/Table/BlogPostsTable.php:683
- 重複チェックが最大10回で打ち切られているため、
*_copy/*_copy_1...*_copy_10が既に存在する場合は重複したままsaveOrFail()に進み、コピーが失敗します。上限を設けない(もしくは既存の最大サフィックスを探索して次を採番する)か、上限に達した場合は明示的に失敗(false返却/例外で理由提示)する処理を入れてください。
for ($i = 1; $i <= 10 && $this->find()->where(['BlogPosts.name' => $data->name])->count(); $i++) {
$suffix = '_' . $i;
$data->name = mb_substr($baseName, 0, 255 - mb_strlen($suffix)) . $suffix;
}
plugins/bc-blog/src/Model/Table/BlogPostsTable.php:725
saveOrFail()はバリデーション失敗時にPersistenceFailedExceptionを投げますが、ここでは捕捉していないため(以前はnameエラー時にリトライしていた)、スラッグ競合が解決できないケースや競合レースで例外が上位へ伝播します。少なくともnameのユニークエラーについてはPersistenceFailedExceptionを捕捉して再採番/再試行するか、呼び出し側が扱いやすい形(false返却 or 400相当の扱いにできる例外)に揃えることを検討してください。
try {
$result = $this->saveOrFail($this->patchEntity($this->newEmptyEntity(), $arrayData));
if ($eyeCatch) {
$result->eye_catch = $eyeCatch;
$this->renameToBasenameFields($result, true);
$result = $this->save($result);
}
// EVENT BlogPosts.afterCopy
$this->dispatchLayerEvent('afterCopy', [
'id' => $result->id,
'data' => $result,
'oldId' => $id,
'oldData' => $oldData,
]);
return $result;
} catch (BcException $e) {
throw $e;
}
Comment on lines
+678
to
+682
| $baseName = mb_substr($data->name, 0, 250) . '_copy'; | ||
| $data->name = $baseName; | ||
| for ($i = 1; $i <= 10 && $this->find()->where(['BlogPosts.name' => $data->name])->count(); $i++) { | ||
| $suffix = '_' . $i; | ||
| $data->name = mb_substr($baseName, 0, 255 - mb_strlen($suffix)) . $suffix; |
| if ($data->name) { | ||
| $baseName = mb_substr($data->name, 0, 250) . '_copy'; | ||
| $data->name = $baseName; | ||
| for ($i = 1; $i <= 10 && $this->find()->where(['BlogPosts.name' => $data->name])->count(); $i++) { |
Comment on lines
+677
to
+685
| if ($data->name) { | ||
| $baseName = mb_substr($data->name, 0, 250) . '_copy'; | ||
| $data->name = $baseName; | ||
| for ($i = 1; $i <= 10 && $this->find()->where(['BlogPosts.name' => $data->name])->count(); $i++) { | ||
| $suffix = '_' . $i; | ||
| $data->name = mb_substr($baseName, 0, 255 - mb_strlen($suffix)) . $suffix; | ||
| } | ||
| } | ||
| $data->title = mb_substr($data->title, 0, 250) . '_copy'; |
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.
よろしくお願いします。