使用Sonata管理bundle在后台主页上显示我的实体的最佳方式是什么



我正在使用SONATA管理捆绑包做一个后台,我想知道你如何在后台端获得不同的实体,以便在主页上获取我所有不同的对象。拥有类似演示的东西可能会非常棒:http://demo.sonata-project.org/admin/dashboard。有没有其他人经历过这件事,可以解释我?

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

所以你需要一些教义课程,但你的服务没有这些。您可以将它们注入到您的 services.yml 中,例如:

sonata.block.service.statistics:
        class: EvinceObjectsBundleBlockServiceStatisticsService
        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在你需要的时候构建你的服务时,它将为你注入原则实体管理器。这称为"依赖注入"。

在执行函数中,您可以执行以下操作:

$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

请参阅symfony网站以获取有关如何使用教义的信息。

最新更新