lepix
lepix

Reputation: 4990

Symfony : should repositories contain only queries to the database?

Let's suppose we have 2 methods in a repository :

public function findByMonth($month) {
    // ... DQL query to the database
}


public function findByYear($year)
   $year = ... ;
   $result = [];

   for (...) {
       $month = ... ;
       $result[] = $this->findByMonth($month);
   }

   return $result;
}   

The second method do not execute query to the database. It only calls the first method and create a loop and some logic adjustments.

Should it stays in the Repository or should it go in a Service (Formatter ? Manager ?)

Upvotes: 0

Views: 148

Answers (1)

Alessandro Minoccheri
Alessandro Minoccheri

Reputation: 35973

Repository is a pattern which represents an architectural layer that handles communication between the application and data source.
In my opinion you need to move findByYear function into a service that call the repository method.

Like this:

class YourService 
{
    private $repository;

    public function __construct(YourRepository $repository)
    {
         $this->repository = $repository;
    }

    public function findByYear($year)
    {
        $year = ... ;
        $result = [];

        for (...) {
            $month = ... ;
            $result[] = $this->repository->findByMonth($month);
        }

        return $result;
    }   
}

Upvotes: 3

Related Questions