首页 > 解决方案 > Symfony 4 manytoone 关系允许在表单构建器中添加

问题描述

我在订阅实体和客户实体之间建立了多方关系。我正在制作表格以向客户添加订阅。在这种形式中,我可以选择一个已经存在的客户,但如果它不存在,我希望能够创建一个新客户。

客户实体:

class Customer implements \JsonSerializable
{
    /**
     * @var int
     *
     * @ORM\Id
     * @ORM\GeneratedValue
     * @ORM\Column(type="integer")
     */
    private $id;

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

    public function getId(): ?int
    {
        return $this->id;
    }

    public function setName(string $name): void
    {
        $this->name = $name;
    }

    public function getName(): ?string
    {
        return $this->name;
    }

    /**
     * {@inheritdoc}
     */
    public function jsonSerialize(): string
    {
        return $this->name;
    }

    public function __toString(): string
    {
        return $this->name;
    }
}

订阅实体:

class Subscription
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\ManyToOne(targetEntity="App\Entity\Customer")
     * @ORM\JoinColumn(nullable=false)
     */
    private $customerName;  

    public function getId(): ?int
    {
        return $this->id;
    }

    /**
     * @return CustomerName
     */
    public function getCustomerName()
    {
        return $this->customerName;
    }

    public function setCustomerName($customerName): self
    {
        $this->customerName = $customerName;

        return $this;
    }

    public function __toString()
    {
        return $this->customerName;
    }
}

订阅类型(表单):

class SubscriptionType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
         ->add('customerName', null, array(
             'required' => true,
         ));

    }

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

所以目前我可以为现有客户创建订阅,我想知道如何为不存在的客户创建订阅?

标签: symfony

解决方案


Refers to this official doc for complete implementation: https://symfony.com/doc/current/reference/forms/types/collection.html You have to use CollectionType to contains the collection of the customer in your case. Then, in your twig, write some jquery to dynamically add a new customer. Something like:

class SubscriptionType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
         ->add('customers', CollectionType::class, array(
             'entry_type' => CustomerType::class,
             'entry_options' => [
                'allow_add' => true,
                'prototype' => true
             ]
         ));

    }

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

推荐阅读