2017-01-09 128 views
0

也許有一個更簡單的方法來做到這一點。但是我想要顯示在試圖將當前表格輸入到數據庫表時已經找到的條目(URL)。所以在控制器中,我試圖傳遞兩個數組。一個是整個表格,另一個是與表格中的條目匹配的。所以用戶可以看到他們已經存在。Symfony陣列陣列

$repository = $this->getDoctrine()->getRepository('ObjectBundle:object'); 

foreach ($mylinks as &$value) { 
    $linkexist = $repository->findOneByUrl($value); 

    if (!$linkexist) { 
     $obj = new Object(); 
     $obj->setUrl($value); 
     $obj->setLastupdate(new \DateTime('now')); 

     $em = $this->getDoctrine()->getManager(); 
     $em->persist($obj); 
     $em->flush(); 
    } else {  
     $notfound = new Object(); 
     $notfound->setUrl($value); 
    } 
} 

$em = $this->getDoctrine()->getManager(); 
$listurls = $em->getRepository('ObjectBundle:Object')->findAll(); 

return $this->render('object/index.html.twig', array(
    'objects' => $listurls, 
)); 

我想包括$ NOTFOUND到一個單獨的陣列或解析它不改變對象實體。有任何想法嗎?

+0

你的命名錶明你想要別的東西,至少對我來說。 '$ notfound'應該是'$ existing'或者至少'$ found'。 –

回答

1

Object含有某種Id,它可以用在這裏:

$existingIds = array(); 
$k=0; 

然後收集的ID:

} else {  
    $notfound = new Object(); 
    $notfound->setUrl($value); 
    $nfound[$k]=$notfound; 
    $k++; 
} 

傳遞數組:

return $this->render('object/index.html.twig', array(
    'objects' => $listurls, 
    'existingIds' => $existingIds 
)); 

最後,在你的Twig,你會有這樣的事情:

{% if existingIds is defined %} 
    {% for existingId in existingIds %} 
     {{ existingId.url }} 
    {% endfor %} 
{% endif %} 

希望這有助於有點...

+0

感謝您的想法。我只需要添加一個數組來保存$ NOTFOUND的網址,然後傳遞作爲一個單獨的數組是這樣的: 回報$這個 - >渲染(「的Proofpoint/index.html.twig」,陣列( \t \t \t \t 'proofpoints'=> $ listurls,'notfounds'=> $ nfound)); 我要修改你的回覆,然後給你信用。謝謝!! – shayster01

+0

當然,很高興我可以幫助:) –

+0

雖然,你**真的**不需要'$ k' :) –