使用 Sonata Admin Bundle 在后台主页显示我的实体的最佳方式是什么

What is the best way to display my entities at Back-office home page with the Sonata Admin Bundle

我正在使用 SONATA ADMIN BUNDLE 做后台工作,我想知道您如何在后台获取不同的实体以在主页上获取我所有的不同对象。拥有像演示这样的东西可能会非常棒:http://demo.sonata-project.org/admin/dashboard。 有没有其他人有过这方面的经验并可以解释一下?

嗯,我想您在 services.yml 中定义了一项服务。在该服务中,您需要获取所需的信息..

所以你需要一些原则class,但你的服务没有这些。你可以将它们注入你的services.yml,例如:

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

在您的服务中,您必须实现一个 'setDoctrine' 函数:

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

还有一个 getDoctrine 函数:

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

现在,当 symfony 在您需要时构建您的服务时,它会为您注入 doctrine entity manager.. 这称为 'Dependency Indjection'。

在执行函数中,你可以这样做:

$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

有关如何使用 doctrine 的信息,请参阅 symfony 网站。