【发布时间】:2014-11-19 14:05:16
【问题描述】:
我想按日期时间变量对对象数组进行排序,日期位于未来,我想将最接近当前日期的日期作为第一个日期。
我在 symfony2 中使用以下控制器操作:
public function fixturesAction(){
if (false === $this->get('security.context')->isGranted('IS_AUTHENTICATED_FULLY')) {
throw $this->createAccessDeniedException('Unable to access this page!');
}
$user = $this->get('security.context')->getToken()->getUser();
$team = $this->getDoctrine()
->getRepository('LoginLoginBundle:Team')
->findByUserUserid($user->getUserid());
$matchGamesHome = $this->getDoctrine()
->getRepository('LoginLoginBundle:Matchgame')
->findByHometeam($team[0]->getName());
$matchGamesAway = $this->getDoctrine()
->getRepository('LoginLoginBundle:Matchgame')
->findByAwayteam($team[0]->getName());
$matchGames = array_merge($matchGamesHome, $matchGamesAway);
$sorted = usort($matchGames, function($a, $b) {
return $a->date->format('U') - $b->date->format('U');
});
return $this->render('LoginLoginBundle:Default:fixtures.html.twig', array("matchArray"=>$sorted));
}
我在哪里进行排序:
$sorted = usort($matchGames, function($a, $b) {
return $a->date->format('U') - $b->date->format('U');
});
这会产生以下错误:
Error: Cannot access private property Login\LoginBundle\Entity\Matchgame::$date in C:\wamp\www\SocProNetbeans\src\Login\LoginBundle\Controller\DefaultController.php line 675
第 675 行如下:
return $a->date->format('U') - $b->date->format('U');
对这个数组进行排序的正确方法是什么?
【问题讨论】:
标签: php arrays sorting symfony datetime