Как хранить данные из массива в php? - PullRequest
0 голосов
/ 08 мая 2020

Мне нужно получить все сообщения $ для каждого $ sub_val, но проблема в том, что все сообщения $ в foreach разбиты, а я получаю только последнее. $

результат print_r($postcats) равно

Array ( [0] => Array ( [post_id] => 6 ) 
        [1] => Array ( [post_id] => 7 ) )

, а результат print_r($para) равен

Array ( [id] => 6 ) 
Array ( [id] => 7 )
$em = $this->getDoctrine()->getManager();

$repository = $em->getRepository('SiteBundle:Post');
$postcats = $repository->createQueryBuilder('t')
                    ->select('t.id as post_id')
                    ->innerJoin('t.categories', 'c')
                    ->where('c.id = :category_id')
                    ->setParameter('category_id', $getidCat)
                    ->getQuery()
                    ->getResult();


foreach ($postcats as $key => $value) { 
    foreach ($value as $sub_key => $sub_val) { 
        $entitymanager=$this->getDoctrine()->getManager();
        $para=array('id'=>$sub_val);
        print_r($para);
        $posts=$entitymanager->getRepository('SiteBundle:Post')
                            ->findBy($para);
    } 
}
return $this->render('post/index.html.twig', array(
            'posts' => $posts,
            'login' =>$usersesssion,
            )
        );

1 Ответ

0 голосов
/ 08 мая 2020

Я нашел решение, я просто изменил код из моего контроллера и индекса. html .twig Я сделал l oop для

    {   $session=$request->getSession();

        $usersesssion=$session->get('login');
        $getidCat=$category->getId();
    if($getidCat!=null){
        $em = $this->getDoctrine()->getManager();

        $repository = $em->getRepository('SiteBundle:Post');
        $postcats = $repository->createQueryBuilder('t')
                            ->select('t.id as post_id')
                            ->innerJoin('t.categories', 'c')
                            ->where('c.id = :category_id')
                            ->setParameter('category_id', $getidCat)
                            ->getQuery()
                            ->getResult(); 

        $posts= $em->getRepository('SiteBundle:Post')->findAll();
        return $this->render('post/index.html.twig', array(
        'posts' => $posts,
        'login' =>$usersesssion,
        'postcats' =>$postcats,
        ));
    }else{
        echo 'ko';
        $postcats=null;
        $em = $this->getDoctrine()->getManager();
        $posts= $em->getRepository('SiteBundle:Post')->findAll();
        return $this->render('post/index.html.twig', array(
        'posts' => $posts,
        'login' =>$usersesssion,
        'postcats' =>$postcats,
        ));
    }}

...