我有一个朋友列表,应该在一页中显示 3 个。每个朋友都有一个类别,我还有一个下拉菜单可以选择仅查看来自所选类别的朋友。它们也应该在一页中显示 3 个。过滤和未过滤朋友的显示方式是相同的,所以我不想在我的控制器和两个相同的模板中有两个几乎动作,所以我尝试在一个控制器的动作和模板中进行此操作,但是有一个问题。我无法为过滤后的朋友的第二页和后续页进行分页。请帮忙!:(问题是我使用了一个表单,当我点击第二页时,填写在表单中并绑定的变量变得未定义。这是代码:
控制器的动作
public function displayAction($page, Request $request) { $em = $this->getDoctrine()->getEntityManager(); $user = $this->get('security.context')->getToken()->getUser(); $cat = new Category(); $dd_form = $this->createForm(new ChooseCatType($user->getId()), $cat); if($request->get('_route')=='filter') { if($request->getMethod() == 'POST') { $dd_form->bindRequest($request); if($cat->getName() == null) { return $this->redirect($this->generateUrl('home_display')); } $filter = $cat->getName()->getId(); if ($dd_form->isValid()) { $all_friends = $em->getRepository('EMMyFriendsBundle:Friend') ->filterFriends($filter); $result = count($all_friends); $FR_PER_PAGE = 3; $pages = $result/$FR_PER_PAGE; $friends = $em->getRepository('EMMyFriendsBundle:Friend') ->getFilteredFriendsFromTo($filter, $FR_PER_PAGE, ($page-1)*$FR_PER_PAGE); $link = 'filter'; } } } else { $all_friends = $user->getFriends(); $result = count($all_friends); $FR_PER_PAGE = 3; $pages = $result/$FR_PER_PAGE; $friends = $em->getRepository('EMMyFriendsBundle:Friend') ->getFriendsFromTo($user->getId(), $FR_PER_PAGE, ($page-1)*$FR_PER_PAGE); $link = 'home_display'; } // Birthdays $birthdays = null; $now = new \DateTime(); $now_day = $now->format('d'); $now_month = $now->format('m'); foreach ($all_friends as $fr) { if($fr->getBirthday() != null) { if($fr->getBirthday()->format('d') == $now_day && $fr->getBirthday()->format('m') == $now_month) { $birthdays[]=$fr; $fr->setYears(); } } } // Search $search = new Search(); $s_form = $this->createFormBuilder($search) ->add('words', 'text', array( 'label' => 'Search: ', 'error_bubbling' => true)) ->getForm(); // Renders the template return $this->render('EMMyFriendsBundle:Home:home.html.twig', array( 'name' => $name, 'friends' => $friends, 'user' => $user, 'birthdays' => $birthdays, 'pages' => $pages, 'page' => $page, 'link' => $link, 'dd_form' => $dd_form->createView(), 's_form' => $s_form->createView())); }
模板
{% if birthdays != null %} <div> <img class="birthday" src="http://www.clker.com/cliparts/1/d/a/6/11970917161615154558carlitos_Balloons.svg.med.png"> <div class="try"> This friends have birthday today: {% for bd in birthdays %} <p> <a href="{{ path('friend_id', {'id': bd.id}) }}">{{ bd.name }}</a> <span class="years"> ({{ bd.years }} years) </span> </p> {% endfor %} </div> </div> {% endif %} {% for fr in friends %} {# TODO: Fix where are shown #} {% if fr.getWebPath()!=null %} <a href="{{ path('friend_id', {'id': fr.id}) }}"> <img class="avatar" src="{{ fr.getWebPath }}"> </a> {% endif %} {% if loop.index is odd %} <p class="list1"> {% else %} <p class="list2"> {% endif %} <a class="friends" href="{{ path('friend_id', {'id': fr.id}) }}">{{ fr.name }}</a> </p> {% endfor %} {# TODO: Pagination #} {% if pages>1 %} <p> {% for i in 0..pages %} {% if page == loop.index %} <span class="pagination">{{ loop.index }}</span> {% else %} <span class="pagination"><a href="{{ path(link, {'page': loop.index}) }}">{{ loop.index }}</a></span> {% endif %} {% endfor %} </P> {% endif %} <p>Choose category:</p> <form class="search" action="{{ path('filter') }}" method="post" {{ form_enctype(s_form) }}> {{ form_widget(dd_form.name) }} {{ form_rest(dd_form) }} <input type="submit" value="Show friends" /> </form>
存储库
类 FriendRepository 扩展了 EntityRepository { 公共函数 getFriendsFromTo ($user, $limit, $offset) { 返回 $this->getEntityManager() ->createQuery('SELECT f FROM EMMyFriendsBundle:Friend f WHERE f.user='.$user.'ORDER BY f.name ASC') ->setMaxResults($limit) ->setFirstResult($offset) ->getResult(); } 公共函数 filterFriends ($filter) { $q = $this->createQueryBuilder('f');
$q->select('f') ->where('f.category = :filter') ->setParameter('filter', $filter); return $q->getQuery()->getResult(); } public function getFilteredFriendsFromTo ($filter, $limit, $offset) { $q = $this->createQueryBuilder('f'); $q->select('f') ->where('f.category = :filter') ->setMaxResults($limit) ->setFirstResult($offset) ->setParameter('filter', $filter); return $q->getQuery()->getResult(); } }
我尝试了很多东西,但总是有问题。在这段代码中,它说$all_friends
生日 for 循环中的变量没有定义——是的,它没有定义。也许我必须将它存储在会话中,我尝试了这个:
$session = $this->getRequest()->getSession();
$session->set('all_friends');
然后传递$friends=$session->get('all_friends');
给for循环,但它不起作用,并且变量不是$all_friends
太大而无法存储它吗?
任何想法将不胜感激!感谢您的时间和精力!
编辑
当我使用会话的方式和
$session = $this->getRequest()->getSession();
$session->set('all_friends');
$fri=$session->get('all_friends');
foreach ($fri as $fr)
{ .... }
我得到的错误是
Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\MyFriends\src\EM\MyFriendsBundle\Controller\HomeController.php line 100
并且
Warning: Missing argument 2 for Symfony\Component\HttpFoundation\Session::set(), called in C:\xampp\htdocs\MyFriends\src\EM\MyFriendsBundle\Controller\HomeController.php on line 71 and defined in C:\xampp\htdocs\MyFriends\app\cache\dev\classes.php line 148
当我不使用会话时,我得到
Notice: Undefined variable: all_friends in C:\xampp\htdocs\MyFriends\src\EM\MyFriendsBundle\Controller\HomeController.php line 100
当我选择一个类别来显示其中的朋友时,我单击它的第二页。
PS 错误中的行与我粘贴的代码中的行不对应,因为我跳过了操作、存储库和模板的某些部分,因为它们与此问题无关并且它们工作正常。如果有人愿意,我可以发送给他或在此处更新所有代码。