Jack Coolen
Jack Coolen

Reputation: 199

How can I access the configuration pool in Symfony Sonata in the Admin class?

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

This is the line that defines the choice field;

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

And this is the enumStatus function;

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".

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.

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?

I make use of Sonata Admin Bundle and Symfony.

Upvotes: 1

Views: 1815

Answers (1)

BENARD Patrick
BENARD Patrick

Reputation: 30975

Please take a look at this official doc.

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

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

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

$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...

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

Upvotes: 1

Related Questions