I have 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;
...
}
I am trying how they behaves. Take a look to the pics below:

In this way I allow to choose more than one command which is why I used a ManyToMany but ...

In this way I shouldn't allow to choose more than one so only one is allowed. Still the same entities but change the behavior based on a condition (the user picks something).
I have tried some config (one by one) for the EasyAdmin entity:
easy_admin:
entities:
Predeterminated:
class: Clanmovil\PlatformAdminBundle\Entity\Predetermined
form:
fields:
...
# not works, still allow pick many commands
- { property: 'commands', label: 'form.command', type_options: { multiple: false } }
# not works, got this error: The value of type "object" cannot be converted to a valid array key.
- { property: 'commands', label: 'form.command', type: 'choice', type_options: { multiple: false } }
# works, but needs to define the class parameter
- { property: 'commands', label: 'form.command', type: 'entity', type_options: { class: 'PlatformAdminBundle\Entity\Command', multiple: false } }
...
Is this a bug? A bad configuration on my side? Anything else?
In the other side, the widget for the working config doesn't display properly:

Help? ping @javiereguiluz @Pierstoval @ogizanagi
Hey @javiereguiluz since I saw you answering and closing a few issues can you give me an advice on this one?
@reypm sorry, I can't at the moment. I'm focused on fixing reported bugs.
@reypm The last solution with the entity type and the class options are correct, but you should specify multiple: true because it's a ManyToMany relationship. I used this in another non-EasyAdmin form this week and this is the right way.
In this way I shouldn't allow to choose more than one so only one is allowed. Still the same entities but change the behavior based on a condition (the user picks something).
Sorry, double-posting, but I just saw this part. Then multiple: false is good and the problem with the widget is quite strange. Did you see a javascript error or something?
@Pierstoval nop, no JS error, weird (the code is on the repo if you want to take a look)

Hello
@reypm I've run on the same problem today i've found that my entity don't go through the configure method in JavierEguiluz\Bundle\EasyAdminBundle\Form\Type\Configurator\EntityTypeConfigurator
so i added the data attribute via the config like this
{ property: 'tag', type: 'entity', type_options: { class: 'MyEntity', multiple: true, attr: { data-widget: 'select2' } } }
i don't know if this feature was intended but this is my solution :)
@snaks12 thanks for providing a solution for this. Could you please also provide some more details about the error? You say that your entity doesn't go through the configure() method of EntityTypeConfigurator but I don't fully understand it. Thanks!
I test easyadmin for not a very long time so i don't know how this part work exactly but when i saw my select don't get the data-widget. I searched where this is added. Next i put a var_dump($options) in configure and it doesn't trigger on the entity with inversBy.
I can't try more than that now sorry :(
@snaks12 no worries, thanks for the info!
I'm closing this one because it's too old. If the problem persists in newer versions of the bundle, let's create new issues. Thanks!
Most helpful comment
Hello
@reypm I've run on the same problem today i've found that my entity don't go through the
configuremethod inJavierEguiluz\Bundle\EasyAdminBundle\Form\Type\Configurator\EntityTypeConfiguratorso i added the data attribute via the config like this
i don't know if this feature was intended but this is my solution :)