Book Image

Symfony2 Essentials

Book Image

Symfony2 Essentials

Overview of this book

Table of Contents (17 chapters)
Symfony2 Essentials
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Data collectors


Now, it's time to discover a way to create your own profile section. Adding this might be useful if you are developing your own bundle and you want to give other developers an easy way to debug your code. To add the profile section, you need to create a new data collector. Let's assume that we want to track information about how many tasks and tags we have in the database.

Add the following code:

<?php

// src/AppBundle/Collector/TaskDataCollector
namespace AppBundle\Collector;

use Doctrine\ORM\EntityManager;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\DataCollector\DataCollector;

class TaskDataCollector extends DataCollector
{
    /**
     * @var EntityManager
     */
    protected $em;

    public function __construct(EntityManager $em)
    {
        $this->em = $em;
    }

In the constructor, we will inject entity manager through our service container configuration:

    public function...