SF2中的表单集合

时间:2011-07-13 19:43:21

标签: doctrine-orm symfony

Offers.php

/**
 * @var \ZGoffers\MainBundle\Entity\OfferParameter
 *
 * @ORM\OneToMany(targetEntity="OfferParameter", mappedBy="offer", cascade={"all"})
 */
private $parameters;

OfferParameter.php

/**
 * @var \ZGoffers\MainBundle\Entity\Offer
 *
 * @ORM\ManyToOne(targetEntity="Offer", inversedBy="offer", cascade={"all"})
 */
private $offer;

OfferType.php

class OfferType extends AbstractType
{
    public function buildForm(FormBuilder $builder, array $options)
    {
        $builder
            ->add('advertiser')
            ->add('name')
            ->add('url', 'text', array('label' => 'URL'))
            ->add('externalUrl', 'text', array('label' => 'External URL'))
            ->add('dailyCap', 'text', array('label' => 'Daily Cap'))
            ->add('parameters', 'collection', array(
                'type' => new OfferParameterType(),
                'allow_add' => true,
                'allow_delete' => true
            ))
            ->add('active', 'choice', array(
                'choices' => array(0 => 'Disabled', 1 => 'Enabled')
            ));
    }

    public function getDefaultOptions(array $options)
    {
        return array(
            'data_class' => 'ZGOffers\MainBundle\Entity\Offer'
        );
    }
}

OfferParameterType.php

class OfferParameterType extends AbstractType
{
    public function buildForm(FormBuilder $builder, array $options)
    {
        $builder
            ->add('field')
            ->add('type', 'choice', array(
                'choices' => array(
                    '='  => 'EQUALS',
                    '>'  => 'IS GREATER THAN',
                    '>=' => 'IS GREATER THAN OR EQUALS',
                    '<'  => 'IS LESS THAN',
                    '<=' => 'IS GREATER THAN OR EQUALS'
                )
            ))
            ->add('value');
    }

    public function getDefaultOptions(array $options)
    {
        return array(
            'data_class' => 'ZGOffers\MainBundle\Entity\OfferParameter'
        );
    }
}

表格处理

public function process()
{
    if ('POST' == $this->request->getMethod()) {
        // bind form data
        $this->form->bindRequest($this->request);

        // If form is valid
        if ($this->form->isValid() && ($offer = $this->form->getData()) instanceof Offer) {

            foreach ($offer->getParameters() as $parameter) {
                $parameter->setOffer($offer); // THIS SHOULDNT BE NEEDED
            }

            // save offer to the database
            $this->entityManager->persist($offer);
            $this->entityManager->flush();

            return true;
        }
    }

    return false;
}

我的问题是......你到底怎么删除SF2中的表单集合中的元素????

我的项目中有多个表单就像这个表单一样,它真的停止了开发:(

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

您可能需要执行以下操作:

->add('parameters', 'collection', array(
                'type' => new OfferParameterType(),
                'allow_add' => true,
                'allow_delete' => true,
                'attr'=>array('style'=>'display:none;')
            ))

这会隐藏您的整个OfferParameterType集合。

或者在OfferParameterType中为每个字段应用相同的内容,如下所示:

->add('field',null, array('attr'=>array('style'=>'display:none;')))