[英]Symfony2 get methods on collection
我想在集合上使用get方法-但它不起作用。
$user = $this->getUser(); // Single User object -> all fine!
$abc = $user->getABC(); // getABC is a collection of multiple objects -> still fine!
$random = $abc->getRandom(); // method getRandom not working on collection -> ERROR Call to undefined method Doctrine\ORM\PersistentCollection::getRandom()
我嘗試了一個foreach循環,但是我沒有設法使其工作。
有什么辦法告訴symfony2在此集合的每個對象上使用getRandom方法?
最后,我要列出由用戶創建的所有條目。 如果您需要更多信息->評論! =)
問候
為什么不使用:
$random = $abc->get(rand(0, $abc->count() - 1));
參考: http : //www.doctrine-project.org/api/common/2.3/class-Doctrine.Common.Collections.ArrayCollection.html
foreach ($abc as $rand) {
$random = $rand->getRandom();
}
只是所有元素的簡單循環。 這是可能的,因為ArrayCollection
實現了IteratorAggregate
接口。 查看PHP文檔
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.