Pez
Pez

Reputation: 1299

Symfony2 where should I put logic for a dashboard page tracking statistics of entity generation?

In my Symfony2 project I have an entity type called "question".

Questions originate from a finite number of sources, at the moment there are two sources.

I want to build a dashboard page just to give an overview of how many questions are coming in from each source. EG: Total questions from source in last hour, last 24 hours, average number of questions per minute, per hour, per day, per week. This weeks total vs. last weeks total. Etc. etc.

I'm not sure what the neatest way to do this is to fit with Symfony2's architecture. Should I add my custom queries to my QuestionRepository? Or, should I just create a custom service called something like QuestionReports, and add custom queries to it? Or...something else?

Thank you!

Upvotes: 1

Views: 129

Answers (1)

A.L
A.L

Reputation: 10513

I suggest you to keep a simple solution, add new methods in your QuestionRepository, for example getQuestionsFrom($dateStart), getAverageNumberOfQuestionsPer($period), etc.

Then call these methods in your controller and pass the results as parameters:

public function dashboardAction()
{
    $dt = new \Datetime("now");
    $dt->modify('-1 hour');

    $questions_from_last_hour = $this->getDoctrine()
        ->getRepository('AcmeWebsiteBundle:Question')
        ->getQuestionsFrom($dt);

    $dt = new \Datetime("now");
    $dt->modify('-24 hour');

    $questions_from_last_24h = $this->getDoctrine()
        ->getRepository('AcmeWebsiteBundle:Question')
        ->getQuestionsFrom($dt);

    // ...

    $twig_template = 'AcmeWebsiteBundle:Default:dashboard.html.twig';

    return $this->render($twig_template,
        array(
            'questions_from_last_hour' => $questions_from_last_hour,
            'questions_from_last_24h' => $questions_from_last_24h,
            // ...
        )
    );
}

Upvotes: 1

Related Questions