Using Symfony 4.1, Doctrine 2.x, I have three Entities. One Subscription can have Many Identities assigned (aka User accounts). When saving which Identities belong to the currently visible Subscription on screen (via standard Symfony form), it fails.
Identity.php
class Identity implements UserInterface, \Serializable, InteractiveLoginUserInterface
{
/**
* @var int
*
* @ORM\Column(type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string|null
*
* @ORM\Column(type="string", length=255, unique=true)
* @Assert\NotBlank()
* @Assert\Email()
* @Assert\Length(max="255")
*/
public $username;
Subscription.php
class Subscription
{
/**
* @ORM\Column(type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
public $id;
/**
* @var bool
* @ORM\Column(type="boolean")
*/
/**
* @var Collection
* @ORM\OneToMany(targetEntity="SubscriptionIdentity", mappedBy="subscription", fetch="EAGER")
*/
public $identities;
SubscriptionIdentity.php
class SubscriptionIdentity
{
/**
* @var int
*
* @ORM\Column(type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORM\ManyToOne(targetEntity="Subscription", inversedBy="identities")
* @ORM\JoinColumn(name="subscription_id", referencedColumnName="id", nullable=false)
*/
public $subscription;
/**
* @ORM\ManyToOne(targetEntity="Identity", inversedBy="subscriptions")
* @ORM\JoinColumn(name="identity_id", referencedColumnName="id", nullable=true)
*/
public $identity;
I have the following Form
class GeneralSettingsType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
....<snip>....
->add('identities', EntityType::class, [
'label' => 'Who can view this subscription',
// looks for choices from this entity
'class' => Identity::class,
//'placeholder' => 'Choose an option',
// uses the User.username property as the visible option string
'choice_label' => 'username',
'multiple' => true,
This works for for displaying a list box of options for the end user to select from:
When I save my form I get this issue:
ORMInvalidArgumentException
HTTP 500 Internal Server Error
Expected value of type "App\Entity\**SubscriptionIdentity**" for association field
"App\Entity\Subscription#$identities", got "App\Entity\Identity" instead.
Please help! Symfony / Doctrine documentation is not overly helpful in this case. Thanks.
You get this error because you try to assign a object of type Identity
to a attribut where doctrine waiting for un object off type SubscriptionIdentity
.
What you describe here is a many to many relation.
Try to modify your entities like this:
class Subscription
{
/**
* @ORM\Column(type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
public $id;
/**
* @var bool
* @ORM\Column(type="boolean")
*/
/**
* @var Collection
* @ManyToMany(targetEntity="Identity")
* @JoinTable(name="subscription_identity",
* joinColumns={@JoinColumn(name="subscription_id", referencedColumnName="id")},
* inverseJoinColumns={@JoinColumn(name="identity_id", referencedColumnName="id")}
* )
*/
public $identities;