简体   繁体   中英

How to override sonata admin controller to provide fields of another object

I'm quite new with Symfony and particularly with Sonata Admin. In my project, I have an entity Project, with a OneToMany relation with a CommunicationDetails entity. This CommunicationDetails entity is linked to a DataSource entity with a ManyToOne relation.

A DataSource is a third-application I have to call to get information about a project. A project can be linked with many data sources. Each communication (project-datasource) needs a project_datasource_id, which is the project's ID in the 3rd party app.

/**
* @ORM\Table(name="project")
* @ORM\Entity
*/
class Project {
    ...

    /**
     * @ORM\OneToMany(targetEntity="CommunicationDetails", mappedBy="project", cascade={"persist"}, indexBy="project")
     */
    private $details;
}



/**
* @ORM\Table(name="communication_details")
* @ORM\Entity;
*/
class CommunicationDetails {

...

/**
 * @ORM\ManyToOne(targetEntity="Project", inversedBy="details")
 */
private $project;

}



/**
 * @ORM\Table(name="datasource")
 * @ORM\Entity
 */
class DataSource{

...

/**
 * @ORM\OneToMany(targetEntity="CommunicationDetails", mappedBy="dataSource", cascade={"persist"}, indexBy="dataSource")
 */
private $details;

I've no problem to manage this behavior with Sonata but I would like to improve the creation process a little bit. I would like to provide fields of the CommunicatonDetails entity when I'm creating a project. In fact, I'm looking for something like this (even if I know that I can't do it this way) :

$mapper
->with('General', ['class' => 'col-md-4'])
    ->add('name', TextType::class)
    ->add('description', TextareaType::class)
    // Here, I'd like to provide fields of the CommunicationDetails entity to create these objects in the same time
    ->add('details.project_datasource_id', TextType::class)
    ->add('details.basePath', TextType::class)
->end();

I read a lot of articles concerning Sonata and its CRUDController, and I think I have to implement it this way. However, I didn't find how to do it. I had a look to the basic sonata template, which I'll have to override too, but every form element displayed are linked with my model, so I've no idea how I could add my own fields. If someone has some link to give, or any idea, I would be really grateful !

PS : I did my best, sorry for my english errors.

Since "details" is a collection, you can't do it that way, thats right. But in sonata admin there are some additional types like 'sonata_type_admin' which are used to embed simply another admin of the desired type to your current form.

->add('details', 'sonata_type_admin')

Look here for another examples and types

https://symfony.com/doc/2.3/bundles/SonataAdminBundle/reference/form_types.html#sonata-type-admin

So it depends on your purpose what form type to use.

That's exactly what I needed, thanks a lot ! I've to use CollectionType because of my relations but it's really what I needed.

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