12

我在玩 Symfony2 并且我不确定 Symfony2 如何处理 View 组件中的多态集合。似乎我可以创建一个包含 AbstractChildren 集合的实体,但不确定如何在 Form Type 类中使用它。

例如,我有以下实体关系。

/**
 * @ORM\Entity
 */
class Order
{
    /**
     * @ORM\OneToMany(targetEntity="AbstractOrderItem", mappedBy="order", cascade={"all"}, orphanRemoval=true)
     * 
     * @var AbstractOrderItem $items;
     */
    $orderItems;  
    ...
}


/**
 * Base class for order items to be added to an Order
 *
 * @ORM\Entity
 * @ORM\InheritanceType("JOINED")
 * @ORM\DiscriminatorColumn(name="discr", type="string")
 * @ORM\DiscriminatorMap({
 *     "ProductOrderItem" = "ProductOrderItem",
 *     "SubscriptionOrderItem " = "SubscriptionOrderItem "
 * })
 */
class AbstractOrderItem
{
    $id;
    ...
}

/**
 * @ORM\Entity
 */
class ProductOrderItem  extends AbstractOrderItem
{
    $productName;
}

/**
 * @ORM\Entity
 */
class SubscriptionOrderItem extends AbstractOrderItem
{
    $duration;
    $startDate;
    ...
}

很简单,但是当我为我的订单类创建表单时

class OrderType extends AbstractType
{
    public function buildForm(FormBuilder $builder, array $options)
    {
        $builder->add('items', 'collection', array('type' => AbstractOrderItemType()));
    }
}

我不确定如何处理这种情况,您实际上需要为集合中的每个项目类别使用不同的表单类型?

4

2 回答 2

9

我最近解决了一个类似的问题——Symfony 本身对多态集合没有做出任何让步,但是使用 EventListener 来扩展表单很容易为它们提供支持。

下面是我的 EventListener 的内容,它使用了与 Symfony\Component\Form\Extension\Core\EventListener\ResizeFormListener 类似的方法,事件监听器提供了集合表单类型的正常功能:

namespace Acme\VariedCollectionBundle\EventListener;

use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;

class VariedCollectionSubscriber implements EventSubscriberInterface
{
    protected $factory;
    protected $type;
    protected $typeCb;
    protected $options;

    public function __construct(FormFactoryInterface $factory, $type, $typeCb)
    {
        $this->factory = $factory;
        $this->type = $type;
        $this->typeCb = $typeCb;
    }

    public static function getSubscribedEvents()
    {
        return array(
            FormEvents::PRE_SET_DATA => 'fixChildTypes'
        );
    }

    public function fixChildTypes(FormEvent $event)
    {
        $form = $event->getForm();
        $data = $event->getData();

        // Go with defaults if we have no data
        if($data === null || '' === $data)
        {
            return;
        }

        // It's possible to use array access/addChild, but it's not a part of the interface
        // Instead, we have to remove all children and re-add them to maintain the order
        $toAdd = array();
        foreach($form as $name => $child)
        {
            // Store our own copy of the original form order, in case any are missing from the data
            $toAdd[$name] = $child->getConfig()->getOptions();
            $form->remove($name);
        }
        // Now that the form is empty, build it up again
        foreach($toAdd as $name => $origOptions)
        {
            // Decide whether to use the default form type or some extension
            $datum = $data[$name] ?: null;
            $type = $this->type;
            if($datum)
            {
                $calculatedType = call_user_func($this->typeCb, $datum);
                if($calculatedType)
                {
                    $type = $calculatedType;
                }
            }
            // And recreate the form field
            $form->add($this->factory->createNamed($name, $type, null, $origOptions));
        }
    }
}

使用这种方法的缺点是它要在提交时识别多态实体的类型,您必须在绑定之前将表单上的数据设置为相关实体,否则侦听器无法确定数据的真正类型是。您可以使用 FormTypeGuesser 系统解决这个问题,但这超出了我的解决方案的范围。

同样,虽然使用此系统的集合仍然支持添加/删除行,但它会假定所有新行都是基本类型 - 如果您尝试将它们设置为扩展实体,它会给您一个关于包含的表单的错误额外的字段。

为简单起见,我使用便利类型来封装此功能 - 请参见下面的示例:

namespace Acme\VariedCollectionBundle\Form\Type;

use Acme\VariedCollectionBundle\EventListener\VariedCollectionSubscriber;
use JMS\DiExtraBundle\Annotation\FormType;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\AbstractType;

/**
 * @FormType()
 */
class VariedCollectionType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        // Tack on our event subscriber
        $builder->addEventSubscriber(new VariedCollectionSubscriber($builder->getFormFactory(), $options['type'], $options['type_cb']));
    }

    public function getParent()
    {
        return "collection";
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setRequired(array('type_cb'));
    }

    public function getName()
    {
        return "varied_collection";
    }
}

示例:命名空间 Acme\VariedCollectionBundle\Form;

use Acme\VariedCollectionBundle\Entity\TestModelWithDate;
use Acme\VariedCollectionBundle\Entity\TestModelWithInt;
use JMS\DiExtraBundle\Annotation\FormType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\AbstractType;

/**
 * @FormType()
 */
class TestForm extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $typeCb = function($datum) {
            if($datum instanceof TestModelWithInt)
            {
                return "test_with_int_type";
            }
            elseif($datum instanceof TestModelWithDate)
            {
                return "test_with_date_type";
            }
            else
            {
                return null; // Returning null tells the varied collection to use the default type - can be omitted, but included here for clarity
            }
        };

        $builder->add('demoCollection', 'varied_collection', array('type_cb' => $typeCb,  /* Used for determining the per-item type */
                                                                   'type' => 'test_type', /* Used as a fallback and for prototypes */
                                                                   'allow_add' => true,
                                                                   'allow_remove' => true));
    }

    public function getName()
    {
        return "test_form";
    }
}
于 2012-07-23T11:02:00.960 回答
-1

在您给出的示例中,您必须为那些 ProductOrder 和 SubscriptionOrder 创建不同的表单类

class ProductOrderType extends AbstractType
{
    public function buildForm(FormBuilder $builder, array $options)
    {
        //Form elements related to Product Order here
    }
}

class SubsciptionOrderType extends AbstractType
{
    public function buildForm(FormBuilder $builder, array $options)
    {
        //Form elements related SubscriptionOrder here
    }
}

在您的 OrderType 表单类中,您添加这两种表单,如下所示

class OrderType extends AbstractType
{
    public function buildForm(FormBuilder $builder, array $options)
    {
         $builder->add('product',new ProductOrderType())
         $builder->add('subscription',new SubsciptionOrderType())
        //Form elements related to order here
    }
}

现在这将两个表单 SubsciptionOrderType,ProductOrderType 添加到主表单 OrderType 。因此,稍后在控制器中,如果您初始化此表单,您将获得订阅和产品表单的所有字段以及 OrderType 的字段。

如果仍然不清楚,我希望这能回答您的问题,请在此处查看嵌入多个表单的文档。http://symfony.com/doc/current/cookbook/form/form_collections.html

于 2012-07-23T09:48:27.943 回答