今日、再帰を伴う無名関数に対してArrayCollection :: forAllメソッドを使用すると、私は奇妙な動作を発見しました。ArrayCollection :: forAll with recursion
前提条件:
私はPost
エンティティのコレクションを持っています。各Post
には、SocialPost
エンティティのコレクションが含まれています。
目的: "保留" するすべてのポストとSocialPostエンティティのため
設定状況。
私のソリューション:
は私がこのように、非常に単純な閉鎖を使用することができることを考えた:
$setPending = function($_, StatusAwareInterface $post) use (&$setPending) {
echo "func entry point reached\r\n";
if ($post instanceof Post) {
echo "This is post. SP Count: " . count($post->getSocialPosts()) . "\r\n";
$post->getSocialPosts()->forAll($setPending);
$status = Post::STATUS_PENDING;
} else {
echo "This is SP\r\n";
$status = SocialPost::STATUS_PENDING;
}
$post->setStatus($status);
};
// $post contains 2 Post entities
// Each Post entity contains 50+ SocialPost entities
$posts->forAll($setPending);
結果:
しかし、出力は非常に奇妙です。 FORALLが唯一の最初の項目を使用し、その後破るように見えます:
func entry point reached
This is post. SP Count: 52
func entry point reached
This is SP
は、誰もがここで問題を見ていますか?
はい、あなたは正しいです。私はarray_walkとまったく同じように動作すると仮定して、このメソッドを使用していました。ありがとうございました。 – Hast