embed from symfony 2.3.8 - Null when allow_add is

2019-07-15 06:20发布

问题:

I try to save new data (when I modify, it's works ok) in embed form, but allow_add doesn't work.

When i send data throw prototype inputs, the request->request is:

ads[adsCapPublisher][new_created_0][publisher]:2302
ads[adsCapPublisher][new_created_0][dailyLimit]:2
ads[adsCapPublisher][edit_0][publisher]: 5201
ads[adsCapPublisher][edit_0][dailyLimit]: 5

The edit_0 was good procesed and creates the AdsType with the AdsCapPublisherType, but when i added new inputs throws javascript, when procesed the data, fails in $request->handleRequest():

Catchable fatal error: Argument 1 passed to addAdsCapPublisher() must be an instance of AdsCapPublisher, null given in Ads.php on line 721

The data is null. And when if I set allow_add to false, the edit entities are procesed good but the news return a form error:

this form should not contain extra fields

Here my clases:

Ads.php

class Ads{

 /**
 * @ORM\OneToMany(targetEntity="AdsCapPublisher", mappedBy="ad", cascade={"persist", "remove"})
 **/
protected $adsCapPublisher;


public function addAdsCapPublisher(AdsCapPublisher $adCapPublisher)
{
    $this->adsCapPublisher[] = $adCapPublisher;

    return $this;
}

public function removeAdsCapPublisher(AdsCapPublisher $adCapPublisher)
{
    $this->adsCapPublisher->removeElement($adCapPublisher);
}

public function getAdsCapPublisher()
{
    return $this->adsCapPublisher;
}

public function setAdsCapPublisher($adsCapPublisher)
{
    $this->adsCapPublisher = $adsCapPublisher;

    return $this;
}

[...]
}

AdsCapPublisher.php

class AdsCapPublisher{


/**
 * @var Users
 *
 * @ORM\ManyToOne(targetEntity="Users", fetch="LAZY")
 * @ORM\JoinColumn(name="id_publisher", referencedColumnName="id_user")
 */
protected $publisher;

/**
 * @var decimal $dailyLimit
 *
 * @ORM\Column(name="daily_limit", type="decimal", nullable=false)
 */
protected $dailyLimit;

[...]
}

AdsType.php

public function buildForm(FormBuilderInterface $builder, array $options)
{
        $entityId= $builder->getData();
        $builder->add('adsCapPublisher', 'collection', array(
            'type' => new AdsCapPublisherType(),
            'required' => false,
            'allow_add' => true,
            'allow_delete' => true,
            'by_reference' => false
        ))

AdsCapPublisherType.php

class AdsCapPublisherType extends AbstractType{

public function buildForm(FormBuilderInterface $builder, array $options)
{
    // -- SECTION: General

    $builder
        ->add('publisher', 'hidden', array(
            'attr' => array(
                'class' => 'js-field-popup-publisher-added'
            )))
        ->add('dailyLimit', 'text', array(
            'required' => false,
            'attr' => array(
                'class' => 'js-field-popup-budget-added form-control k-input-field k-is-input-text k-ellipsis k-has-currency-inside-input',
                'min' => 0,
            )
        ));
}

public function getName()
{
    return 'adsCapPublisher';
}

public function setDefaultOptions(OptionsResolverInterface $resolver)
{
    $resolver->setDefaults(array(
        'csrf_protection' => false,
        'data_class' => 'Entity\AdsCapPublisher',
        'csrf_field_name' => '_token',
        'intention' => 'ads',
        'translation_domain' => 'ads',
        'empty_data' => null
    ));
}

}

Any help? :S