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;
}
}
yhaaa這是真的:) - – Nitin
但如果我想搜索創建日期的日期和時間的基礎上,它顯示排序,但同一日期創建的筆記顯示降序.. :(是有任何解決方案預先感謝.. – Nitin
這取決於你希望他們如何排序,但你可以添加到排序(第二個)數組。如果,例如,你想按標題排序,你可以使用'array('creationdate '=>'ASC','title'=>'ASC')' – qooplmao