简体   繁体   中英

get repository from Admin Class SonataAdminBundle

I work with symfony 2.8 and SonataAdminBundle, I want to view the users who are 'client' and here is my code:

ClientAdmin

 protected function configureListFields(ListMapper $listMapper)
{

     $result = $this->getConfigurationPool()->getContainer()->get('Doctrine')->getRepository('UserBundle:User')->findClient();

    $listMapper

        ->add('client', 'sonata_type_model', array(
            'empty_value' => '', 
            'choice_list' => $result))

              ;
}

UserRepository

public function findClient()
{   $dql = "SELECT p FROM UserBundle:User p WHERE p.type LIKE 'client' ORDER BY p.id DESC";
    return $this->getEntityManager()
        ->createQuery($dql)

        ->getResult();
}

But it does not work and still no result

You can customize the list query thanks to the createQuery method :

    <?php

public function createQuery($context = 'list')
{
    $query = parent::createQuery($context);
    $query->andWhere(
        $query->expr()->eq($query->getRootAliases()[0] . '.my_field', ':my_param')
    );
    $query->setParameter('my_param', 'my_value');
    return $query;
}

Sonata Documentation

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM