0

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?

1
  • Your naming suggests you want something else, at least to me. The $notfound should really be $existing or at least $found. Commented Jan 9, 2017 at 21:45

1 Answer 1

1

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...

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for the idea. I just needed to add an array to hold the $notfound URLs and then pass that as a separate array like this: return $this->render('proofpoint/index.html.twig', array( 'proofpoints' => $listurls,'notfounds' => $nfound)); I am going to modify your response and then give you credit. Thanks!!
Sure, glad I could help :)
Though, you really do not need that $k :)
Tried that but just wrote over first entry in array.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.