2017-01-09 110 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' :) –