I am trying to optimize the architecture of my web-application developed in PHP using the Symfony framework.

I have an object "pool" that contains "swimmers". I need to display the number of swimmers in my pool, but only those with a pink bath-suit.

In order to do this, I see many possible approaches :


  1. Create a custom getter method in my Pool class, and filters the swimmers by bath suits color :

    public function getSwimmersInPinkSuit() {
        for each($this->getSwimmers() as $swimmer) {
            if($swimmer->getBathSuitColor() == "pink") {
                 $pinkSwimmers[] += $swimmer;
        return $pinkSwimmers;

    {{ pool.getSwimmersInPinkSuit | length }}

  2. Create a custom SQL query in my controller, define the number there before sending it to my template.

  3. Add a "$pinkSwimmers" property to my Pool class, and increment/decrement it with an eventListener every time I persist in database the event "a pinkSwimmer joins/leaves the pool". I then can access the value where i want :

    {{ pool.pinkSuits }}

  4. Use a denser twig request to filter out the pink swimmers directly in my template

    {{ pool.swimmers.suitColor("pink") | lenght }} // If that's even possible o0


In the four propositions above, I see pros and cons to each of them, so :

  • What is the best practice in this situation ? Am I overseeing a better logic in addition to the ones mentioned above ? I try to find an implementation that would be lightweight in term of performance (memory, bandwidth), but also easy to reconfigure (change from pink to green suits, or from suit color to swimming style).
  • 1
    What do you mean by optimize? What is the problem? – JeffO Feb 24 '14 at 20:01
  • What is the nature of the data (that is, how many swimmers, how many different types of swimmers do you expect to be filtering on, etc.)? – Jay Elston Feb 24 '14 at 20:30
  • @JeffO I try to find the best implementation of such filtering - it is only a performance problem. In the first approach for instance, I am not very comfortable with a for loop triggered on all the swimmers everytime i need to display a simple int... In the 3rd approach, I am not very comfortable with a relatively heavy development only for a simple counter (in term or reusability, maintenance etc.). – np1987 Feb 25 '14 at 9:31
  • @JayElston There can be up to ~250 swimmers in a pool, and the swimmer object has a reasonable number of properties (25+) that make it quite "heavy" in term of data volume. Symfony has a dev bar that loads a lot of usefull data about a page when developping locally (details about DB requests, mapping informations, response time etc.). If I get more than 50 swimmers in a page for instance, it can easilly trigger a memory overflow exception (the dev bar is out of memory) that makes me think that I'm currently using the wrong approach (I use the first approach right now). – np1987 Feb 25 '14 at 9:39

From the comments, it appears as though you are searching 250 swimmers with 25 properties each.

Even if this is done by application code, it should still take an unnoticeable amount of time, unless you are making N queries per swimmer, or any sort of complex lookup per swimmer. If this is the case, look up the N+1 select problem and use the minimum number queries/lookups to build your swimmers.

SO answer on N+1 select

PHP article on N+1 select

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.