2

I have this two entities Command and Predetermined and they are related as ManyToMany:

class Command
{
    use IdentifierAutogeneratedTrait;
    use TimestampableEntity;
    use ActiveTrait;

    /**
     * @var string
     * @ORM\Column(type="string", length=150)
     */
    private $name;

    /**
     * @var string
     * @ORM\Column(type="text", nullable=true)
     */
    private $description;

    /**
     * @var Predetermined[]
     * @ORM\ManyToMany(targetEntity="Predetermined", mappedBy="commands", cascade={"persist"})
     */
    private $predetermined;

    ...
}

class Predetermined
{
    use IdentifierAutogeneratedTrait;
    use TimestampableEntity;
    use ActiveTrait;

    /**
     * @var string
     * @ORM\Column(type="string", length=160)
     * @Assert\NotBlank()
     */
    private $smsText;

    /**
     * @var Command[]
     * @ORM\ManyToMany(targetEntity="Command", inversedBy="predetermined", cascade={"persist", "remove"})
     * @ORM\JoinTable(name="predetermined_has_command")
     */
    private $commands;

    ...
}

Is there any way, using a form (is the only one I am seeing here) to change how they behaves? Take a look to the pics below:

2016-01-27_22-57-56

I allow here to choose many commands which is why I used a ManyToMany but ...

2016-01-27_22-58-11

in this one I doesn't so I still need to use same entities but change the behavior at form level. Can any give me some help on this? How do I achieve this?

4
  • Do you want allow multiple choices in the first and restrict to one in the second form ?
    – chalasr
    Jan 29, 2016 at 21:36
  • @chalasr yes, you got it! That's exactly what I want
    – ReynierPM
    Jan 29, 2016 at 21:37
  • Do you have a FormType (or use of FormBuilder in the method displaying the form) ? If yes, please add it in your question
    – chalasr
    Jan 29, 2016 at 21:39
  • @chalasr no I don't have. I am using EasyAdminBundle but the principle is the same I just need to see how to achieve this using a Form and then I can implement as EasyAdmin does
    – ReynierPM
    Jan 29, 2016 at 21:40

1 Answer 1

1

Normally use the multiple attribute in your FormBuilder's field, like

$builder->add('commands', 'entity', array(
    'class'     => 'YourBundle:Command',
    'multiple'  => yourCondition ? true : false,
));

I've found the EasyAdminBundle way from this PR, you should be able to use the following

// app/config/admin/entities/YourEntity
fields:
    - { property: 'commands', multiple: false }

For one form, and set it to false for another.
Or you can define a parameter like :

- { property: 'commands', multiple: %condition% }
8
  • That makes sense, I will try and come back with test results, thanks
    – ReynierPM
    Jan 29, 2016 at 21:47
  • 1
    I don't know EasyAdminBundle, but it works in SonataAdmin (which uses the symfony FormBuilder)
    – chalasr
    Jan 29, 2016 at 21:51
  • Yup, you're right, that allow to define the selection to be many but what about when I need to choose one? Here is the docs but I am not sure how to override the form. Also I am not following on your latest update
    – ReynierPM
    Jan 29, 2016 at 22:12
  • Set multiple to false doesn't restrict the maximum choice to one ? Also you can use a parameter like in my last edit, and set it dynamically using your condition
    – chalasr
    Jan 29, 2016 at 22:14
  • Hmmm ... are you defining the type of your field ? Maybe you have to specify type choice for use the multiple attribute ? Or entity
    – chalasr
    Jan 29, 2016 at 22:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.