从Symfony 2中的ArrayCollection获取随机元素的问题

Xav*_*avi 3 arrays fixtures symfony doctrine-orm

有人可以帮帮我吗?我在尝试加载DataFixtures时遇到此错误:

Notice: Undefined index:  in path/DataFixtures/ORM/Fixture.php
Run Code Online (Sandbox Code Playgroud)

这是我的简单代码......我不明白为什么会失败,因为我之前使用另一个夹具做了它并且它起作用了

$things = $manager->getRepository('namespaceBundle:Entity')->findByProperty($property->getId());
$thing = $things[array_rand($things)];
Run Code Online (Sandbox Code Playgroud)

我只是想从ArrayCollection中获取一个随机对象 $things

我需要一些帮助,我被困了......我确定这是一个愚蠢的错误,但我看不到它!

谢谢!

Mic*_*hal 10

最简单的是

$thing = $things->get(array_rand($things->toArray())));
Run Code Online (Sandbox Code Playgroud)