Doctrine 2: Using Criteria is quicker than filtering collections

I recently realized that using filters on Doctrine collections is much slower than using criterias. So I’ll just put this here as a reminder for myself and anybody else who might be looking for it.

This is slow:

<?php

namespace Acme\Bundle\AppBundle\Entity;

use Doctrine\ORM\Mapping AS ORM;

class User
{
    /**
     * @ORM\OneToMany(targetEntity="Acme\Bundle\AppBundle\Entity\Task", mappedBy="user")
     */
    protected $tasks;

    public function getTasks()
    {
        return $this->tasks;
    }
   
    public function getCompletedTasks()
    {
        return $this->getTasks()->filter(function($task){
            return $task->getCompleted();
        });
    }
}

This is fast:

<?php

namespace Acme\Bundle\AppBundle\Entity;

use Doctrine\Common\Collections\Criteria;
use Doctrine\ORM\Mapping AS ORM;

class User
{
    /**
     * @ORM\OneToMany(targetEntity="Acme\Bundle\AppBundle\Entity\Task", mappedBy="user")
     */
    protected $tasks;

    public function getTasks()
    {
        return $this->tasks;
    }
   
    public function getCompletedTasks()
    {
        $criteria = Criteria::create()->where(Criteria::expr()->eq('completed', true));

        return $this->getTasks()->matching($criteria);
    }
}

Sonntagslinks, Vol. 26

Living in Switzerland ruined me for America and its lousy work culture (vox.com)
«In Switzerland, you don’t arrive to a meeting late, but you also don’t leave for your lunch break a second past noon. If it’s summer, jumping into the lake to swim with the swans is an acceptable way to spend your lunch hour. If you eat a sandwich at your desk, people will scold you.»

Ein Haiexperte im Interview: «Es ist ein Kampf, den wir führen müssen» (migrosmagazin.ch)
«Bemerkenswert ist, dass man sich Haien überhaupt derart nähern kann, mit Löwen geht das nicht. Wer im Krüger-Nationalpark in Südafrika aus dem Auto heraus einen Löwen sieht und aussteigt, ist tot.»

Entrepreneurs don’t have a special gene for risk—they come from families with money (qz.com)
«The most common shared trait among entrepreneurs is access to financial capital—family money […] and connections that allow for access to financial stability. »

Anderswo

Von mir, auf anderen Kanälen.

Four More Uncool Steps To A Successful Startup (medium.com)
«Following seemingly uncool, conservative steps proved to be a working recipe to launch successful companies.»

Sonntagslinks, Vol. 25

What are some pivotal moments that every successful SaaS company has? (quora.com)
Eine Liste von 10 Schritten, die jedes erfolgreiche Startup durchläuft:
«The first 10 Unaffiliated Customers. This won’t feel like much, but it’s a big deal. You’ve gotten 10 folks you’ve never met to actually pay for your product. This really is amazing. Amazing.»

Ein absurder Absturz (blog.bernerzeitung.ch)
«Die [FIFA-]Weltrangliste ist ein Witz.»
Siehe dazu auch diesen Artikel.

Making video look like film (gypsycreative.com)
«We’re talking about upgrading from the “home video” look to something viewers are more accustomed to, and giving your work an aesthetic that adds depth to the story.»

Kreative in Bern: „Wir geben weiter, was uns Spaß macht!“ (spiegel.de)
«Nach Zürich – sofort. Aber nach Bern, in die brave Schweizer Bundesstadt? Unbedingt! Junge Kreative versetzen mit ihren Ideen und ihrer Heimatliebe die ganze Stadt in Sommerlaune.»