0
votes

I am doing a back-office with the SONATA ADMIN BUNDLE and I was wondering how you get your different entities in the back-office side to get all my different objects at home-page. It's could be very great to have something like the demo : http://demo.sonata-project.org/admin/dashboard. Has someone else experienced about this and can explain me?

1
See dashboard section in docs and how to create a sonata block and register in dashboardM Khalid Junaid
I ever did my differents blocks and know i need to match with my entities to display the differents features that i would like to have on the home-page.Vincent Moulene
If someone have an idea, i follow this tutorial to create my different block : sonata-project.org/bundles/block/master/doc/reference/… and I would to get my elements (entities) but I never used the services and Doctrine. And i would like to work in the right direction.Vincent Moulene
I am following this : stackoverflow.com/questions/15966575/… and : stackoverflow.com/questions/24229141/…. To test it, i try with a statistic block. In my case with messages. So the block is in right place but now i need to inform with the exact number of messages. Has someone had an example to help me.Vincent Moulene

1 Answers

0
votes

Well, I guess you have a service defined in your services.yml. In that service you need to grab the information you want..

So you'll need some doctrine class, but your service doesn't have these.. You can inject them in your services.yml, for example:

sonata.block.service.statistics:
        class: Evince\ObjectsBundle\Block\Service\StatisticsService
        tags:
            - { name: sonata.block }

        arguments:
            - ~
            - '@templating'
        calls:       
            - [ setDoctrine, ["@doctrine.orm.entity_manager"]] # <- here add the doctrine entity manager

In your service you have to implement an 'setDoctrine' function:

private $doctrine;
/**
 * @param mixed $doctrine
 */
public function setDoctrine($doctrine)
{
    $this->doctrine = $doctrine;
}

And a getDoctrine function:

/**
 * @return mixed
 */
public function getDoctrine()
{
    return $this->doctrine;
}

Now, when symfony builds your service when you need it, it will inject the doctrine entity manager for you.. This is called 'Dependency Indjection'.

In the execute function you can do something like this:

$repo = $this->getDoctrine()->getRepository('AcmeYourBundle:YourEntityClass');
$query = $repo->createQueryBuilder()
          ->from('foo', 'f')
          ->where('foo.bar = :id')
          ->setParameter('id', $someId)
          ->getQuery();
$results = $query->getResult();
$resultCount = count($results);

//-> pass the resultCount to your template

See the symfony website for information how to use doctrine.