Symfony One-to-Many and Many-to-One Form Creator

I know this is possible, but I lost where to start. My main problem is how to customize form classes.

The objects

Formula (id, code, name, formulaColors)

FormulaColor (formula, color, percentage)

Color (id, code, name, formulaColors)

I want to display a multiple choice checklist for a given formula that displays each color in a grid of a table. The grid will have a column that displays the code, name and check box for each color. These are probably not important details. The user should be able to check the flags and send and save it to add control values ​​to Formula-> formulaColors, ultimately adding them to the FormulaColor table. In addition, colors that already belong to the formula must be checked when creating the form. I think it would be easier for me if the FormulaColor table was a real join table without a percentage field.

In a controller action, I have access to a formula for which colors should be selected.

What do my class classes look like, at least roughly?

This is what my form class looks like this

class ChooseColorsType extends AbstractType { public function buildForm(FormBuilderInterface $builder, array $options) { $builder->add('formulaColors', 'entity', array( 'class' => 'PrismPortalCommonBundle:Color', 'property' => 'code', 'expanded' => true, 'multiple' => true, )); } public function setDefaultOptions(OptionsResolverInterface $resolver) { $resolver->setDefaults(array( 'data_class' => 'Prism\Portal\CommonBundle\Entity\Formula' )); } public function getName() { return 'prism_portal_adminbundle_choosecolorstype'; } } 

However, this gives me the exception "Error: calling the undefined Prism \ Portal \ CommonBundle \ Entity \ Color :: getPercentage () method in /vagrant/src/Prism/Portal/CommonBundle/Entity/Formula.php line 201"

I assume this is due to the fact that this is not a regular many-to-many relationship. I do not know how to address this getPercentage () exception

Update

I still have no opportunity to try, but tomorrow. I think this may be the answer to my problem: http://www.prowebdev.us/2012/07/symfnoy2-many-to-many-relation-with.html

I did not think about adding a mutator and accessory to my formula object for directly handling color.

+4
source share

Source: https://habr.com/ru/post/1478942/


All Articles