symfony2 + 嵌入表单 oneToMany 不起作用



我有两个实体组织和CustomValue。对多的关系。我一直在想2天为什么在我的嵌入形式不工作的关系。它在DB中创建两个表,但没有外键。我哪里错了?我也和许多许多人有关系,它在组织实体上运行得很好。

组织:

BaseBundleEntityOrganization:
type: entity
table: base_organizations
gedmo:
    soft_deleteable:
        field_name: deletedAt
        time_aware: false
id:
    id:
        type: integer
        id: true
        generator:
            strategy: AUTO          
oneToMany:
    childOrganizations:
        targetEntity: Organization
        mappedBy: parentOrganization
    customValues:
        targetEntity: CustomValue
        mappedBy: organization
        nullable: false
        cascade: ["persist", "merge"]
manyToOne:
    parentOrganization:
        targetEntity: Organization
        inversedBy: childOrganizations
        nullable: true
manyToMany:
    addresses:
        targetEntity: Address
        inversedBy: organizations
        joinTable: 
            name: base__organizations_addresses
            joinColumns: 
                organization_id:
                    referencedColumnName: id
            inverseJoinColumns:
                address_id:
                    referencedColumnName: id
        cascade: ["persist","merge"]

CustomValues:

BaseBundleEntityCustomValue:
type: entity
table: base_custom_values
id:
    id:
        type: integer
        id: true
        generator:
            strategy: AUTO
fields:
    name:
        type: string
        length: 64
    value:
        type: string
        length: 64
manyToOne:
    organization:
        targetEntity: Organization
        inversedBy: customValues
        nullable: true

OrganizationType.php:

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('name', null, array('max_length' => 100))
        ->add('type')
        ->add(
            'parentOrganization',
            'entity', 
            array(
                'class'         =>  "BaseBundle:Organization",
                'property'      =>  'name',
                'query_builder' => function(EntityRepository $er)
                    {
                        return $er->createQueryBuilder('o')
                            ->select('o')
                            ->add('groupBy', 'o.name');
                    }, 
                'required'  =>  false
            ))
        ->add('addresses', 'collection', array(
            'type'          => new AddressType(),
            'allow_add'     => true,
            'by_reference'  => false
        ))
        ->add('customValues', 'collection', array(
            'type'          => new CustomValueType(),
            'allow_add'     => true,
            'by_reference'  => false
        ));
}

OrganizationController.php:

public function createAction(Request $request)
{
    $entity = new Organization();
    $form = $this->createCreateForm($entity);
    $form->handleRequest($request);
    if ($form->isValid()) {
        $this->get('session')->getFlashBag()->add(
            'success',
            'Your changes were saved!'
        );
        $em = $this->getDoctrine()->getManager();
        $em->persist($entity);
        $em->flush();
        return $this->redirect($this->generateUrl('organization_show', array('id' => $entity->getId())));
    }
    return $this->render('BaseBundle:Organization:new.html.twig', array(
        'entity' => $entity,
        'form'   => $form->createView(),
    ));
}
private function createCreateForm(Organization $entity)
{
    $form = $this->createForm(new OrganizationType(), $entity, array(
        'action' => $this->generateUrl('organization_create'),
        'method' => 'POST',
    ));
    $form->add('submit', 'submit', array('label' => 'Create'));
    return $form;
}
public function newAction()
{
    $entity = new Organization();
    $cv = new CustomValue();
    $entity -> getCustomValues()->add($cv);
    $form   = $this->createCreateForm($entity);
    return $this->render('BaseBundle:Organization:new.html.twig', array(
        'entity' => $entity,
        'form'   => $form->createView(),
    ));
}

对于关系组织<->地址,它可以工作,但这里不是。我不知道,怎么了。你看到什么了吗?

所以表结构中缺少外键,或者它存在,但关系id仍然为空?我的第一个建议是尝试从Organization类中删除setter-getter-add-remove函数,并运行app/console doctrine:generate:entities BaseBundle:Organization。我也遇到过类似的问题,结果发现我在一个访问器函数中有一个错别字。这只是一个提示:(

我已经找到解决办法了。问题是,我必须自己在customValue中添加引用。这意味着我必须添加这些行:

foreach ( $entity->getCustomValuesn() as $cv ) {
       $cv->setOrganization($entity);
    }

这里是控制器:

if ($form->isValid()) {
    foreach ( $entity->getCustomValuesn() as $cv ) {
       $cv->setOrganization($entity);
    }
    $this->get('session')->getFlashBag()->add(
        'success',
        'Your changes were saved!'
    );
    $em = $this->getDoctrine()->getManager();
    $em->persist($entity);
    $em->flush();

这是一些解释,我发现:
https://github.com/symfony/symfony/issues/3201
http://forum.symfony-project.org/viewtopic.php?f=23& t = 35914

最新更新