symfony - Stop SonataAdmin / Symfony2 from creating empty objects with sonata_type_admin embedded admins -


first of all, i'm not sure if sonata issue or symfony2 one, first time i'm working sf2 forms edit relationship.

here's problem:

i have 2 classes, let's call them old favourites: car , wheel. car has optional one-to-one relationship wheel (it's example, go it...). have set sonataadmin caradmin class embeds wheeladmin using sonata_type_admin , try create car without entering data wheel.

however, on submit (somewhere in $form->bind()/$form->submit() far can trace) symfony and/or sonata instantiating wheel , trying persist (with values null). since wheel has non-null constraints throws dbalexception complaining cannot insert wheel null vlaues.

this naughty , i'd stop happening. if don't enter details wheel don't want phantom wheel menacing code , database. expect if enter no data there nothing insert/persist it's left alone. not what's happening... any ideas how tame sensible?


here's long version, code blocks , everything:

the orm definitions first:

# myns\mybundle\resources\config\doctrine\car.orm.yml myns\mybundle\entity\car:   type: entity   repositoryclass: myns\mybundle\entity\repositories\carrepository   table: test_cars   id:     id:       type:                     integer       generator:                { strategy: auto }   fields:     color:       type:                     string       length:                   50     owner:       type:                     string       length:                   50       nullable:                 true   onetoone:     leftfrontwheel:       targetentity:             wheel       cascade:                  [ persist ]       joincolumn:         name:                   leftfrontwheelid         referencedcolumnname:   id   # myns\mybundle\resources\config\doctrine\wheel.orm.yml myns\mybundle\entity\wheel:   type: entity   repositoryclass: myns\mybundle\entity\repositories\wheelrepository   table: test_wheels   id:     id:       type:                     integer       generator:                { strategy: auto }   fields:     diameter:       type:                     integer       length:                   5 

then sonataadmin classes:

namespace myns\mybundle\admin  use ...  class caradmin extends admin {     protected function configureformfields(formmapper $formmapper)     {         $formmapper             ->add('color',              null, array('required' => true))             ->add('owner',              null, array('required' => false))             ->add('leftfrontwheel',     'sonata_type_admin', array('delete' => false))         ;     }      protected function configurelistfields(listmapper $listmapper) { ... } } 

and

namespace myns\mybundle\admin;  use ...  class wheeladmin extends admin {     protected function configureformfields(formmapper $formmapper)     {         $formmapper             ->add('diameter',   null,   array('required' => false))         ;     }      protected function configurelistfields(listmapper $listmapper) { ... } } 

and admin.yml entries:

services:     sonata.admin.car:         class: myns\mybundle\admin\caradmin         tags:             - { name: sonata.admin, manager_type: orm, label: "car" }         arguments:             - ~             - myns\mybundle\entity\car             - 'sonataadminbundle:crud'         calls:             - [ settranslationdomain, [myns\mybundle]]     sonata.admin.wheel:         class: myns\mybundle\admin\wheeladmin         tags:             - { name: sonata.admin, manager_type: orm, label: "wheel" }         arguments:             - ~             - myns\mybundle\entity\wheel             - 'sonataadminbundle:crud'         calls:             - [ settranslationdomain, [myns\mybundle]] 

expected/required behaviour:

  • display form 3 fields:

    • car.color (required)
    • car.owner (optional)
    • car.wheel.diameter (optional)
  • if car.wheel.diameter left blank no wheel should created , test_cars.leftfrontwheelid should remain null in database

  • if car.wheel.diameter entered wheel should created , linked car (this seems work fine existing config)

the question: how system behave above?

it might caused missing 'required' => false, no ?

protected function configureformfields(formmapper $formmapper) {     $formmapper         ->add('color',              null, array('required' => true))         ->add('owner',              null, array('required' => false))         ->add('leftfrontwheel',     'sonata_type_admin', array('required' => false, 'delete' => false))     ; } 

Comments

Popular posts from this blog

css - Which browser returns the correct result for getBoundingClientRect of an SVG element? -

gcc - Calling fftR4() in c from assembly -

Function that returns a formatted array in VBA -