如何在Admin类中访问Symfony Sonata中的配置池?

时间:2021-04-24 06:45:32

I have a select (or a choice) field that should get all options from a function that returns an array.

我有一个select(或选项)字段,它应该从返回数组的函数中获取所有选项。

This is the line that defines the choice field;

这是定义选择字段的行;

->add('possibilities', 'choice', array('choices' => Crud::enumStatus()))

And this is the enumStatus function;

这是enumStatus函数;

public static function enumStatus()
{
    return array(
        '1' => 'Awaiting Approval',
        '2' => 'Partly Approved',
        '3' => 'Approved',
        '4' => 'Disapproved',
        '5' => 'Complete'
    );
}

What I explained above works perfectly. But what I actually want doesn't work. The principals stay the same.

我上面解释的完美。但我真正想要的是行不通的。校长保持不变。

This is what I want;

这就是我要的;

->add('possibilities', 'choice', array('choices' => Crud::getUsers(array('Marketing', 'Human Resource Management'))))

And the function in the same class as the one mentioned above;

并且功能与上述类相同;

public function getUsers($roles)
{
    $queryBuilder = $this->getConfigurationPool()->getContainer()->get('doctrine')->getManager('admin')
            ->createQueryBuilder();                

    $queryBuilder->select('u.id, u.name')
                         ->from('Qi\Bss\BaseBundle\Entity\Admin\User', 'u')
                         ->innerJoin('u.businessRoles', 'r')
                         ->where('r.name IN (:roles)')
                         ->setParameter('roles', $roles)
                         ->orderby('u.name');        
    $result = $queryBuilder->getQuery()->getResult();       

    $users = array();
    foreach ($result as $key => $value) {
        $users[$value['id']] = $value['name'];
    }

    return $users;        
}

The error when trying what I want;

尝试我想要的时候的错误;

Attempted to call method "getConfigurationPool" on class "Xx\Yyy\QqqBundle\Controller\OrderController".

尝试在类“Xx \ Yyy \ QqqBundle \ Controller \ OrderController”上调用方法“getConfigurationPool”。

The controller that is mentioned in the error message is the controller where the ->add() is for my form, not where those two functions is.

错误消息中提到的控制器是控制器,其中 - > add()用于我的表单,而不是这两个函数的位置。

Why does the first one work, but the second one doesn't? Can somebody please explain this to me? Is it something to do with the static in the one function? And how can I then resolve this issue? What is the configurationPool and how do get it?

为什么第一个工作,但第二个不工作?有人可以向我解释一下吗?它与一个函数中的静态有关吗?那我怎么能解决这个问题呢?什么是configurationPool以及如何获得它?

I make use of Sonata Admin Bundle and Symfony.

我使用了Sonata Admin Bundle和Symfony。

1 个解决方案

#1


1  

Please take a look at this official doc.

请看一下这个官方文档。

ConfigurationPool => configuration pool where all Admin class instances are stored

ConfigurationPool =>配置池,其中存储了所有Admin类实例

$this->getConfigurationPool() I think is a copy paste from an Admin class.

$ this-> getConfigurationPool()我认为是Admin类的复制粘贴。

In order to access configuration pool inside one extending CRUDController, you have to access by his admin property.

要在一个扩展CRUDController中访问配置池,您必须通过其admin属性访问。

$this->admin->getConfigurationPool()

Here is the head of your function :

这是你的功能的负责人:

public function getUsers($roles)
{
    $queryBuilder = $this->admin->getConfigurationPool()->getContainer()->get('doctrine')->getManager('admin')
            ->createQueryBuilder(); 

UPDATE:

更新:

If your controller is a simple symfony controller, so you just have to call doctrine instead of getting throw the admin...

如果您的控制器是一个简单的symfony控制器,那么您只需要调用doctrine而不是抛出管理员...

$queryBuilder = $this->get('doctrine')->getManager('admin')
            ->createQueryBuilder();

#1


1  

Please take a look at this official doc.

请看一下这个官方文档。

ConfigurationPool => configuration pool where all Admin class instances are stored

ConfigurationPool =>配置池,其中存储了所有Admin类实例

$this->getConfigurationPool() I think is a copy paste from an Admin class.

$ this-> getConfigurationPool()我认为是Admin类的复制粘贴。

In order to access configuration pool inside one extending CRUDController, you have to access by his admin property.

要在一个扩展CRUDController中访问配置池,您必须通过其admin属性访问。

$this->admin->getConfigurationPool()

Here is the head of your function :

这是你的功能的负责人:

public function getUsers($roles)
{
    $queryBuilder = $this->admin->getConfigurationPool()->getContainer()->get('doctrine')->getManager('admin')
            ->createQueryBuilder(); 

UPDATE:

更新:

If your controller is a simple symfony controller, so you just have to call doctrine instead of getting throw the admin...

如果您的控制器是一个简单的symfony控制器,那么您只需要调用doctrine而不是抛出管理员...

$queryBuilder = $this->get('doctrine')->getManager('admin')
            ->createQueryBuilder();