shayster01
shayster01

Reputation: 214

Symfony array of arrays

Maybe there is a simpler way of doing this. But I want to display entries (URLs) that were already found when trying to input in to a database table along with the current table. So in the controller I am trying to pass two arrays. One that is of the whole table and another of the entries it found matched the entries in the table. So the user can see they already existed.

$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,
));

I would like to include the $notfound into a separate array or parse it without changing the Object entity. Any ideas?

Upvotes: 0

Views: 541

Answers (1)

Jovan Perovic
Jovan Perovic

Reputation: 20193

You Object contains some sort of Id and it can be used here:

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

Then collect the IDs:

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

Pass the array:

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

Finally, in your Twig, you would have something like this:

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

Hope this helps a bit...

Upvotes: 1

Related Questions