How to access Entity class from Sonata Admin's Admin class?

Muhammed picture Muhammed · Mar 1, 2016 · Viewed 8.9k times · Source

Let me explain: I have an Entity class Item with a method getName() in it, namespace App\Entity. I have an admin class for it in Sonata Admin, namespace App\Admin, and would like to call that method from within, how to do so?

//Symfony2 Entity
...
class Item
{
    public function getName(){
         return $this->name;
    }
}

...
//Sonata Admin class
class ItemAdmin extends Admin
{
  ...
  protected function configureListFields(ListMapper $listMapper){
     //how to access Item class' getName() method from here?
  }
}

EDIT: This works inside configureListFields(), but what about without find() and if with find() only, then how to automatically get 'id'?

 $item=$this->getConfigurationPool()->getContainer()->get('Doctrine')->getRepository('AppBundle:Item')->find('15');
 echo $item->getName();

Answer

yakob abada picture yakob abada · Dec 7, 2017

Simple and easy

$container = $this->getConfigurationPool()->getContainer();
$em = $container->get('doctrine.orm.entity_manager');