Symfony2 Form Builder Select Across Join Table with Metadata

后端 未结 3 2049
广开言路
广开言路 2021-02-03 12:40

I have 3 entities:

[Member] ----OneToMany----> [MemberCategory] ---ManyToOne---> [Category]

This works well as far as fetching results fr

3条回答
  •  长发绾君心
    2021-02-03 13:30

    This is how I resolved this problem in the past (applied to your example) but this is what I figured out so it might not be 100% right for your case.

    First create a MemberCategory form type matching your needs:

    add('priority')
                ->add('category', 'entity',
                        array('property' => 'name',
                            'class' => 'CompanyYourBundle:Category'))
            ;
        }
    
        public function setDefaultOptions(OptionsResolverInterface $resolver)
        {
            $resolver->setDefaults(array(
                'data_class' => 'Company\YourBundle\Entity\MemberCategory'
            ));
        }
    
        public function getName()
        {
            return 'company_yourbundle_membercategorytype';
        }
    }
    

    Then add this form type inside your Member type form:

    add('name')
                ->add('members', 'collection', array(
                    'type' => new MemberCategoryType(),
                    'allow_add' => true,
                    'allow_delete' => true,
                    'by_reference' => false,));
            ;
        }
    
        public function setDefaultOptions(OptionsResolverInterface $resolver)
        {
            $resolver->setDefaults(array(
                'data_class' => 'Company\YourBundle\Entity\Member'
            ));
        }
    
        public function getName()
        {
            return 'company_yourbundle_membertype';
        }
    }
    

    Then you can follow the documentation to add as many categories as you want to your members and assigning a priority every time.

提交回复
热议问题