Search code examples
symfonysymfony4symfony-forms

Symfony 4 form Many to Many using join table with extra attributes


I cannot seem to figure out the way to embed forms together, I have used this SO question to try and get close but all I get is an out of memory error.

On the creation of a Buyer I want to add Contacts at the same time. I see examples of checkboxes for this but I want input fields and a contact might not exist.

Also contacts is a general storage for any contact to which they might be part of different buyers / sellers.

/**
 *
 * @ORM\Table(name="buyer")
 * @ORM\Entity
 */
class Buyer
{
    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer", nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $id;

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


    /**
     * @var string|null
     *
     * @ORM\Column(name="address", type="string", length=60, nullable=true)
     */
    private $address;

    /**
     * @var string|null
     *
     * @ORM\Column(name="city", type="string", length=60, nullable=true)
     */
    private $city;

    /**
     * @var string|null
     *
     * @ORM\Column(name="state", type="string", length=20, nullable=true)
     */
    private $state;

    /**
     * @var int|null
     *
     * @ORM\Column(name="zip", type="integer", nullable=true)
     */
    private $zip;

    /**
     * @ORM\OneToMany(targetEntity="App\Entity\BuyerContact", mappedBy="buyer")
     */
    protected $contacts;

    public function __construct()
    {
        $this->contacts = new ArrayCollection();
    }

...
/**
 *
 * @ORM\Table(name="buyer_contact", indexes={@ORM\Index(name="fk_buyer", columns={"buyer"}), @ORM\Index(name="fk_contact", columns={"contact"})})
 * @ORM\Entity
 */
class BuyerContact
{
    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer", nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $id;

    /**
     * @var Buyer
     *
     * @ORM\ManyToOne(targetEntity="App\Entity\Buyer", inversedBy="contacts")
     * @ORM\JoinColumns({
     *   @ORM\JoinColumn(name="buyer", referencedColumnName="id")
     * })
     */
    private $buyer;

    /**
     * @var Contact
     *
     * @ORM\ManyToOne(targetEntity="App\Entity\Contact")
     * @ORM\JoinColumns({
     *   @ORM\JoinColumn(name="contact", referencedColumnName="id")
     * })
     */
    private $contact;
...
/**
 *
 * @ORM\Table(name="contact")
 * @ORM\Entity
 */
class Contact
{
    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer", nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $id;

    /**
     * @var string|null
     *
     * @ORM\Column(name="job_title", type="string", length=30, nullable=true)
     */
    private $jobTitle;

    /**
     * @var string|null
     *
     * @ORM\Column(name="name", type="string", length=60, nullable=true)
     */
    private $name;

    /**
     * @var string|null
     *
     * @ORM\Column(name="email", type="string", length=120, nullable=true)
     */
    private $email;

class BuyerType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('buyer', TextType::class,[
                'required'=>true,
                'label'=>'Buyer',
                'help'=>'The business name of the buyer'
            ])
            ->add('address', TextType::class, [
                'required'=>false,
                'label'=>'Address 1',
                'help'=>'Address line one.'
            ])
            ->add('city', TextType::class, [
                'required'=>false,
                'label'=>'City',
            ])
            ->add('state', TextType::class,[
                'required'=>false,
                'label'=>'State',
            ])
            ->add('zip', NumberType::class, [
                'required'=>false,
                'label'=>'Zip',
            ])
        ;
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults([
            'data_class' => Buyer::class,
        ]);
    }
}
class BuyerContactType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('buyer')
            ->add('contact')
        ;
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults([
            'data_class' => PublisherContact::class,
        ]);
    }
}


Solution

  • This was my mistake as the entities were generated for an existing application. All I had to do was configure the ManyToMany unidirectional relationship between the Buyer and contacts and everything works as intended.