Inserting and updating data for a website monitoring app
You can optimize it so that it's just 2 DB queries:

    $allSites = $this-&gt;get('SiteRepository')-&gt;CustomQuery(); // Returns Site array indexed by id

    $allStatuses = $this-&gt;get('StatusRepository')-&gt;CustomQuery(); // Returns Status array indexed by id

    $results = $this-&gt;getScanManager()-&gt;scanBatch($allSites); // scans all the sites and returns results array

    $em = $this-&gt;getDoctrine()-&gt;getManager();

    foreach ($results as $result) {

        $history = new History();
        $history-&gt;setSite($allSites[$result['id']]);
        $history-&gt;setTime(date('h:i:s d-m-Y'));
        $history-&gt;setStatus($allStatuses[$result['status']]);
        $history-&gt;setResponseTime($result['speed_download']);

        $em-&gt;persist($history);

        $site = $this-&gt;findSiteById($result['id'], $allSites);
        $site-&gt;setSiteStatus($allStatuses[$result['status']]);
        $site-&gt;setSitePingTime($result['speed_download']);
        $site-&gt;setSiteLastUpdate(date('h:i:s d-m-Y'));
    }

    $em-&gt;flush();

    return $this-&gt;render('default/index.html.twig');


Of course this should be adjusted to your app, if you allways ping all the sites, then it's ok, but i you just ping a few, then it might be inefficient, but at least you get the idea.

BTW, if your setter methods return $this, you could write it in a prettier way:

$history-&gt;setSite($allSites[$result['id']])
   -&gt;setTime(date('h:i:s d-m-Y'))
   -&gt;setStatus($allStatuses[$result['status']])
   -&gt;setResponseTime($result['speed_download']);