Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sonata_type_collection : set default field value from current entity instance

I need help with "sonata_type_collection" : Is there any way to define a default value (here : instance of existing entity) for a "sonata_type_collection" specific field? Or maybe a way to give him parameters?

Let me clarify this : Here is a screenshot of my actual "sonata_type_collection" form once rendered :

enter image description here

Is there any way to make the "Machine" field hold the current instance of the "MachineInfo" entity I am editing instead of the "No selection" text when you click the (very last) "Add" button ?

Here's my current 'configureFormFields" from "MachineInfoAdmin" :

protected function configureFormFields(FormMapper $formMapper)
{
    $formMapper
        ->add('name', 'text', array('label' => 'Nom'))
        ->add('description', 'text', array('label' => 'Description'))
        ->add('internalReference', 'text', array('label' => 'Référence interne'))
    ;

    //Already instantiated
    if ($this->id($this->getSubject())) {
        $formMapper
            ->add(
                'machineParts',
                'sonata_type_collection',
                array(
                    'label'     => "Pièces",
                ),
                array(
                    'edit' => 'inline',
                    'inline' => 'table',
                    'sortable'  => 'position',
                )
            )
        ;
   }
}

I'm really stuck into that one, I hope that a savior can provide me his knowledge to help me m(_ _)m

like image 336
Sir McPotato Avatar asked Oct 08 '15 14:10

Sir McPotato


1 Answers

I found the solution, and it was quite simple, i'm ashamed =__=" I forgot to set a reference to the MachineInfo in the newly instantiated MachinePart

public function addMachinePart(MachinePartsInfo $machineParts) {
    $machineParts->setMachineInfo($this); //Missed this line
    $this->machineParts[] = $machineParts;

    return $this;
}

I hope this can help someone in the future :)

like image 109
Sir McPotato Avatar answered Oct 12 '22 09:10

Sir McPotato