我在下面的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
謝謝你,但現在我得到另一個錯誤:調用成員函數get()在C:\ wamp \ www \ test \ src \ Application \ StockBundle \ Admin \ StockLevelView.php中的非對象行36 – Marek
你上面發佈的代碼看起來不錯,所以這應該是另一個問題 – TautrimasPajarskas
我剛剛爲我的類添加了代碼,並且在[setSecurityContext,[@ security.context]]添加了[setContainer,[@service_container]](也是eddited)並且我正在調用一個非對象的成員函數getUser() – Marek