2

我的包中有一个 services.yml,如下所示。

parameters:
     application_stock.admin.stocklevels: Application\StockBundle\Admin\StockLevelView
#    application_stock.example.class: Application\StockBundle\Example

services:
#    application_stock.example:
#        class: %application_stock.example.class%
#        arguments: [@service_id, "plain_value", %parameter%]
     application_stock:
         class: "%application_stock.admin.stocklevels%"
         tags:
             - { name: sonata.admin, manager_type: orm, group: application_stock, label: stock }
         arguments: [ '', Application\StockBundle\Entity\StockLevels, ApplicationStockBundle:StockLevels ]

但是当我试图将服务更改为

- @security.context
- { null, Application\StockBundle\Entity\StockLevels, ApplicationStockBundle:StockLevels }

然后它向我显示一个错误

ContextErrorException: Notice: Undefined offset: 2 in C:\wamp\www\test\vendor
\sonata-project\admin-bundle\Sonata\AdminBundle\DependencyInjection\Compiler\
AddDependencyCallsCompilerPass.php line 48

有人可以帮我解决这个问题吗?

编辑:

添加后

calls:
    - [setContainer, [@service_container]]
    - [setSecurityContext, [@security.context]]

我收到错误:

Call to a member function getUser() on a non-object in

StockLevelView.php:

<?php
namespace Application\StockBundle\Admin;

use Application\StockBundle\Entity\StockLevels;
use Sonata\AdminBundle\Admin\Admin;
use Sonata\AdminBundle\Form\FormMapper;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Show\ShowMapper;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\RedirectResponse;

use Knp\Menu\ItemInterface as MenuItemInterface;

class StockLevelView extends Admin implements ContainerAwareInterface
{

/**
 * @var ContainerInterface
 */
private $container;

private $adminId;

/**
 * {@inheritDoc}
 */
public function setContainer(ContainerInterface $container = null)
{
    $this->container = $container;
}

public function setSecurityContext($securityContext)
{
    $this->adminId = $securityContext->getToken()->getUser();
}

...rest of code
4

2 回答 2

2

像这样使用它:

application_stock:
    class: "%application_stock.admin.stocklevels%"
    tags:
        - { name: sonata.admin, manager_type: orm, group: application_stock, label: stock }
    arguments: [ '', Application\StockBundle\Entity\StockLevels, ApplicationStockBundle:StockLevels ]
    calls:
        - [setSecurityContext, [@security.context]]

并创建public function setSecurityContext($securityContext) { ... }setter 以在您的管理类中捕获安全上下文。

于 2013-07-26T06:54:57.047 回答
0

您只能注入其他服务定义或参数。不是类名。您必须将您的实体定义为另一个服务才能注入它。也许在prototype范围内。

于 2013-07-26T06:24:35.500 回答