Answer the question
In order to leave comments, you need to log in
How to use child entity class or decorator class in SonataAdminBundle (Symfony3, Doctrine ORM)?
Hello!
There was a need to display in the admin panel (SonataAdminBundle) not bare data, but slightly modified ones. For example, in a list of entities, display concatenated fields, or in ManyToOne and ManyToMany forms, display something special (and not just one of the entity fields).
This is implemented, in general, quite simply: in the corresponding methods of the Admin class, instead of the names of the properties, we indicate the names of our methods:
// в Admin-классе MyEntity
protected function configureListFields(ListMapper $listMapper)
{
$listMapper
// ->addIdentifier('name')
->addIndentifier('someCustomField')
;
}
// в стороннем Admin-классе, добавляя сущность MyEntity по связи ManyToOne
protected function configureFormFields(FormMapper $formMapper)
{
$formMapper
// ->add('foo', ModelType::class, array(
// 'class' => MyEntity::class,
// 'property' => 'name'
// ))
->add('foo', ModelType::class, array(
'class' => MyEntity::class,
'property' => 'someOtherCustomField'
))
;
}
// в Entity-классе
public function getSomeCustomField()
{
return $this->name . ' foo';
}
public function getSomeOtherCustomField()
{
return $this->name . ' bar';
}
/**
* @ORM\Table(name="my_entity")
* @ORM\Entity
*/
class MyEntity
{
}
class MyEntityAdminDecorator extends MyEntity
{
}
my_entity_sonata_admin_service:
class: MyBundle\Admin\MyEntityAdmin
arguments: [~, MyBundle\Entity\MyEntityAdminDecorator, ~] # было MyEntity
tags:
- name: sonata.admin
manager_type: orm
An exception has been thrown during the rendering of a template ("Unable to generate a URL for the named route "..." as such route does not exist.").
class MyEntityAdmin extends AbstractAdmin
{
protected $baseRouteName = 'sonata_my_entity';
protected $baseRoutePattern = 'my-entity';
//...
No entity manager defined for class MyBundle\Entity\MyEntityAdminDecorator
Answer the question
In order to leave comments, you need to log in
No need to inherit anything, decorate and create new methods
For list - custom template
For forms - listeners or types
Those. Doctrine ORM (SUDDENLY) considers it to be some completely different entity for which there is no mapping information.
Sorry, but how else should she react? You have created a new entity in the entities folder and annotated it with an entity. From the fact that it is mapped from a table with which another entity is already mapped, the doctrine is neither hot nor cold.
Of course, because the article is a bit not about your problem. And besides that, you missed the key piece, namely:
** @MappedSuperclass */ abstract class Decorator extends Component
To solve the problem, we already wrote above: you need to make your own template for the field. Pick a bundle like SonataNews, there were definitely custom templates for the sheet.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question