2013-07-25 23 views
1

我在下面的bundle中有一個services.yml。Symfony:使用實體時的服務

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 

回答

1

只要使用這樣的:

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) { ... }二傳手捕捉到的安全上下文中的管理類。

+0

謝謝你,但現在我得到另一個錯誤:調用成員函數get()在C:\ wamp \ www \ test \ src \ Application \ StockBundle \ Admin \ StockLevelView.php中的非對象行36 – Marek

+0

你上面發佈的代碼看起來不錯,所以這應該是另一個問題 – TautrimasPajarskas

+0

我剛剛爲我的類添加了代碼,並且在[setSecurityContext,[@ security.context]]添加了[setContainer,[@service_container]](也是eddited)並且我正在調用一個非對象的成員函數getUser() – Marek

0

只能注入其他服務的定義或parame TER值。不是類名。您必須將實體定義爲另一個服務來注入它。也許在prototype範圍內。