Sonata_type_collection: set the default field value from the current instance of the object

I need help with sonata_type_collection: is there a way to determine the default value (here: an instance of an existing entity) for a specific sonata_type_collection field? Or maybe a way to give it options?

Let me clarify this: Here is a screenshot of my actual form "sonata_type_collection" after rendering:

enter image description here

Is there a way to make the "Machine" field hold the current instance of the "MachineInfo" object, which I edit instead of the text "Without selection" when you click the "Add" button (very last)?

Here are 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 with this, I hope the savior can provide me with his knowledge to help me m (_ _) m

+7
symfony sonata-admin sonata
source share
1 answer

I found a solution, and it was pretty simple, I'm ashamed = __ = I forgot to set the link to MachineInfo in the newly created 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 :)

+6
source share

All Articles