首先,我不确定这是Sonata还是Symfony2,这是我第一次使用Sf2表单编辑关系。这是问题所在:我有两个类,我们称它们为旧的收藏夹:Car和Wheel。 Car与Wheel具有可选的一对一关系(以示例为例,就顺其自然...)。我已经使用CarAdmin类设置了SonataAdmin,该类使用WheelAdmin嵌入了sonata_type_admin,并尝试创建Car而不输入Wheel的任何数据。但是,在提交时(据我所能追踪的,在$ form-> bind()/ $ form-> submit()中的某处),Symfony和/或Sonata正在实例化Wheel并尝试将其持久化(其所有值都为)。由于Wheel具有一些非null约束,因此抛出DBALException,提示您无法插入具有空vlaues的Wheel。这很顽皮,我想阻止它的发生。如果我没有输入Wheel的任何详细信息,我不希望幻像的Wheel危及我的代码和数据库。我期望的是,如果我不输入任何数据,则没有可插入/持久的数据,因此将其保留下来。但这不是正在发生的事情……有什么想法可以将其驯服为明智的东西吗?这是长版本,包含代码块和所有内容:首先是ORM定义:# MyNS\MyBundle\Resources\Config\Doctrine\Car.orm.ymlMyNS\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.ymlMyNS\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然后是SonataAdmin类:namespace MyNS\MyBundle\Adminuse ...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) { ... }}和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) { ... }}最后是admin.yml条目: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]]预期/要求的行为:显示包含三个字段的表单:car.color(必填)car.owner(可选)car.wheel.diameter(可选)如果car.wheel.diameter留为空白,则不应创建Wheel,并且null应保留在数据库中的test_cars.leftFrontWheelId如果输入了car.wheel.diameter,那么应该创建一个Wheel并将其链接到Car(这似乎可以与现有配置配合使用)问题:如何使该系统如上所述运行? 最佳答案 这可能是由于缺少'required' => false引起的,不是吗?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)) ;}关于symfony - 阻止SonataAdmin/Symfony2使用sonata_type_admin嵌入式管理员创建空对象,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/18157485/
10-12 00:20