【发布时间】:2016-07-08 13:05:20
【问题描述】:
今天我发现在对带有递归的匿名函数使用 ArrayCollection::forAll 方法时出现了奇怪的行为。
前提条件:
我有一组Post 实体。每个Post 都包含SocialPost 实体的集合。
目标:
将所有 Post 和 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
有人看到这里的问题吗?
【问题讨论】:
标签: php symfony recursion doctrine closures