2013-12-17 99 views
4

//假設實體註釋具有屬性'creationdate'&'getCreationDate()'方法來訪問。根據日期和時間對Symfony中的對象數組進行排序

DefaultController extends Controller { 
     public function indexAction(){ 
      $em = $this->getDoctrine()->getManager(); 
      $repository = $em->getRepository('Bundle:Notes'); 
      $notes = $repository->findBy(array('userid' => $userId); 
      //Now I want to sort the notes array as per creation date using usort 
      usort($notes, array($this,"cmp")); 
     } 
    function cmp($a, $b) { 
     return strtotime($a->getCreationDate()) > strtotime($b->getCreationDate())? -1:1; 
    } 
} 

回答

1

您可以在通話設置爲存儲庫,而不是之後,像這樣......

$notes = $repository->findBy(
    array('userid' => $userId), // search criteria 
    array('creationdate' => 'ASC') // order criteria 
); 

我知道你說你想使用usort但它似乎有點多餘。

+0

yhaaa這是真的:) - – Nitin

+0

但如果我想搜索創建日期的日期和時間的基礎上,它顯示排序,但同一日期創建的筆記顯示降序.. :(是有任何解決方案預先感謝.. – Nitin

+0

這取決於你希望他們如何排序,但你可以添加到排序(第二個)數組。如果,例如,你想按標題排序,你可以使用'array('creationdate '=>'ASC','title'=>'ASC')' – qooplmao

相關問題