簡體   English   中英

以數組格式symfony2訪問多對多關系

[英]Access to many-to-many relation in array format, symfony2

在symfony中,我們可以使用getter函數訪問多對多關系,這些函數返回ArrayCollection類型的對象。 例如,為了獲得Alex的學生,我們可以調用$alex->getStudens() ,然后我可以訪問ale的studens對象。

現在我的問題是我如何訪問數組中alex的學生ID,例如,通過調用$alex->getStudentsIds()它將返回{1,5,7,12,..},這是他的學生ID。

確切地說,您在實體中添加了另一個功能

public function getStudentsIds()
{
   $students = $this->students;
   $studentIds = [];

   foreach($students as $student)
   {
     $studentIds[] = $student->getId();
   }  

   return $studentIds;
}

理想的解決方案是將這種方法添加到存儲庫中,並使其僅查詢給定對象的學生ID,但這是最簡單的解決方案。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM