sebbo
sebbo

Reputation: 2939

How to embed child entities in sonata admin show views?

I am using symfony2 with the sonata admin bundle. Now I have an entity which has OneToOne association to another entity. I want the embed the show form of the child entity into the show form of the parent entity.

I have tried the form type "sonata_type_admin" but that only gives me a link to the sub entity.

Does anybody have a clue how to embed the show form of the sub entity?

Entity definitions:

Parent:

class Parent
{

    /**
     * @ORM\OneToOne(targetEntity="Child", mappedBy="parent")
     */
    private $child;
}

Child:

class Child
{

    /**
     * @ORM\OneToOne(targetEntity="Parent", mappedBy="child")
     * @ORM\JoinColumns({
     *   @ORM\JoinColumn(name="parent_id", referencedColumnName="id")
     * })
     */
    private $parent;
}

Upvotes: 1

Views: 2186

Answers (1)

M Khalid Junaid
M Khalid Junaid

Reputation: 64476

Since you have a property in your parent entity which refers to your child entity you can call child fields in your configureShowFields methods to show fields from your child entity

protected function configureShowFields(ShowMapper $showMapper)
{
    $showMapper
        ->with('Child Fields', array('collapsed' => true))
        ->add('child.propertyName',null,array('label'=>'Label'))
        ->add('child.anotherPropertyName',null,array('label'=>'Label'))
        ... ;
}

Upvotes: 3

Related Questions