Empty data from the array collection after submitting

I need help to save an array collection in the table.
Whenever I submit the form, the collection collection does not save and, when debugging to check what data is being passed, it shows that no data is coming. As if the user had left the field blank.

Here’s my code:

RelFiscProcessosDeTrabalho.php

<?php

namespace AdminRelatorioFiscalizacaoBundleEntity;

use DoctrineCommonCollectionsArrayCollection;
use DoctrineORMMapping as ORM;

/**
 * RelFiscProcessosDeTrabalho
 *
 * @ORMTable(name="rel_fisc_processos_de_trabalho")
 * @ORMEntity(repositoryClass="AdminRelatorioFiscalizacaoBundleRepositoryRelFiscProcessosDeTrabalhoRepository")
 */
class RelFiscProcessosDeTrabalho
{
    /**
     * @var int
     *
     * @ORMColumn(name="id", type="integer")
     * @ORMId
     * @ORMGeneratedValue(strategy="AUTO")
     */
    private $id;

   /**
     * @var RelFiscInstituicoesMunicipio
     *
     * @ORMOneToMany(targetEntity="AdminRelatorioFiscalizacaoBundleEntityRelFiscInstituicoesMunicipio", mappedBy="procTrabInstituicoes", cascade={"ALL"})
     */
    private $instTotalMunicipios;

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

    /**
     * Get id.
     *
     * @return int
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * @return RelFiscInstituicoesMunicipio
     */
    public function getInstTotalMunicipios()
    {
        return $this->instTotalMunicipios;
    }

    /**
     * @param RelFiscInstituicoesMunicipio $instTotalMunicipios
     */
    public function setInstTotalMunicipios($instTotalMunicipios)
    {
        $this->instTotalMunicipios = $instTotalMunicipios;
        return $this;
    }

    function addInstTotalMunicipio($instTotalMunicipio)
    {
        $instTotalMunicipio->setProcTrabInstituicoes($this);
        $this->instTotalMunicipios->add($instTotalMunicipio);
    }

    function removeInstTotalMunicipio($instTotalMunicipio)
    {
        $this->instTotalMunicipios->removeElement($instTotalMunicipio);
        $instTotalMunicipio->setProcTrabInstituicoes(null);
    }
}

RelFiscInstituicoesMunicipio.php

<?php

namespace AdminRelatorioFiscalizacaoBundleEntity;

use DoctrineORMMapping as ORM;

/**
 * RelFiscInstituicoesMunicipio
 *
 * @ORMTable(name="rel_fisc_instituicoes_municipio")
 * @ORMEntity(repositoryClass="AdminRelatorioFiscalizacaoBundleRepositoryRelFiscInstituicoesMunicipioRepository")
 */
class RelFiscInstituicoesMunicipio
{
    /**
     * @var
     *
     * @ORMColumn(name="id", type="integer")
     * @ORMId
     * @ORMGeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var RelFiscProcessosDeTrabalho
     *
     * @ORMManyToOne(targetEntity="AdminRelatorioFiscalizacaoBundleEntityRelFiscProcessosDeTrabalho")
     * @ORMJoinColumns({
     *   @ORMJoinColumn(name="processo_de_trabalho_instituicoes_id", referencedColumnName="id")
     * })
     */
    private $procTrabInstituicoes;

    /**
     * @var Municipio
     *
     * @ORMManyToOne(targetEntity="CoreBundleGeografiaBundleEntityMunicipio")
     * @ORMJoinColumns({
     *   @ORMJoinColumn(name="municipio_ibge", referencedColumnName="ibge")
     * })
     */
    private $municipio;

    /**
     * @var integer
     *
     * @ORMColumn(name="inst_mun_total_instituicoes_fiscalizadas_municipio", type="integer", nullable=true)
     */
    private $instMunTotalInstituicoesFiscalizadasMunicipio;


    /**
     * Get id.
     *
     * @return
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * @return RelFiscProcessosDeTrabalho
     */
    public function getProcTrabInstituicoes()
    {
        return $this->procTrabInstituicoes;
    }

    /**
     * @param RelFiscProcessosDeTrabalho $procTrabInstituicoes
     */
    public function setProcTrabInstituicoes($procTrabInstituicoes)
    {
        $this->procTrabInstituicoes = $procTrabInstituicoes;

        return $this;
    }

    /**
     * @return Municipio
     */
    public function getMunicipio()
    {
        return $this->municipio;
    }

    /**
     * @param Municipio $municipio
     */
    public function setMunicipio($municipio)
    {
        $this->municipio = $municipio;

        return $this;
    }

    /**
     * @return
     */
    public function getInstMunTotalInstituicoesFiscalizadasMunicipio()
    {
        return $this->instMunTotalInstituicoesFiscalizadasMunicipio;
    }

    /**
     * @param   $instMunTotalInstituicoesFiscalizadasMunicipio
     */
    public function setInstMunTotalInstituicoesFiscalizadasMunicipio($instMunTotalInstituicoesFiscalizadasMunicipio)
    {
        $this->instMunTotalInstituicoesFiscalizadasMunicipio = $instMunTotalInstituicoesFiscalizadasMunicipio;

        return $this;
    }
}

RelFiscProcessosDeTrabalhoType.php

<?php

namespace AdminRelatorioFiscalizacaoBundleForm;

use SymfonyComponentFormAbstractType;
use SymfonyComponentFormExtensionCoreTypeCollectionType;
use SymfonyComponentFormExtensionCoreTypeMoneyType;
use SymfonyComponentFormExtensionCoreTypeHiddenType;
use SymfonyComponentFormFormBuilderInterface;
use SymfonyComponentOptionsResolverOptionsResolver;
use SymfonyComponentFormExtensionCoreTypeDateTimeType;
use SymfonyComponentFormExtensionCoreTypeDateType;
use SymfonyComponentFormExtensionCoreTypeTimeType;
use AdminRelatorioFiscalizacaoBundleEntityRelFiscProcessosDeTrabalho;

class RelFiscProcessosDeTrabalhoType extends AbstractType
{
    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
            $builder
                ->add('instTotalMunicipios', CollectionType::class, array(
                    'label' => 'MunicĂ­pios',
                    'entry_type' => RelFiscInstituicoesMunicipioType::class,
                    'translation_domain' => 'EntityTranslations',
                    'allow_add' => true,
                    'allow_delete' => true,
                    'by_reference' => false,
                    'horizontal' => false,
                    'widget_add_btn' => array(
                        'label' => 'Adicionar',
                    ),
                    'entry_options' => array(
                        'label' => false,
                        'widget_form_control_class' => 'row',
                        'municipios' => $options['municipios'],
                        'widget_remove_btn' => array(
                            'horizontal_wrapper_div' => array(
                                'class' => "col-md-1 m-t-25 text-right"
                            ),
                            'wrapper_div' => false,
                        ),
                    ),
                ));
    }


    /**
     * @param OptionsResolver $resolver
     */
    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => RelFiscProcessosDeTrabalho::class,
            'municipios' => array(),
            'crt' => false,
            're' => false,
            'totalInstituicoesFiscalizadasAnterior' => 0,
            'denunciasNaoApuradasEstoqueAnterior' => 0,
            'valor' => 0,
            'profissionaisEnfAnterior' => 0,
            'totalMunicipiosFiscalizadosAnterior' => 0
        ));
    }
}

RelFiscInstituicoesMunicipioType.php

<?php

namespace AdminRelatorioFiscalizacaoBundleForm;

use SymfonyComponentFormAbstractType;
use SymfonyComponentFormFormBuilderInterface;
use SymfonyComponentOptionsResolverOptionsResolver;
use SymfonyComponentFormExtensionCoreTypeDateTimeType;
use SymfonyComponentFormExtensionCoreTypeDateType;
use SymfonyComponentFormExtensionCoreTypeTimeType;
use AdminRelatorioFiscalizacaoBundleEntityRelFiscInstituicoesMunicipio;

class RelFiscInstituicoesMunicipioType extends AbstractType
{
    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('municipio', null, array(
                'label' => 'MunicĂ­pio',
                'translation_domain' => 'EntityTranslations',
                'horizontal' => false,
                'choices' => $options['municipios'],
                'widget_form_group_attr' => array(
                    'class' => 'col-md-5'
                ),
                'attr' => array(
                    'class' => 'select-municipios'
                ),
                'placeholder' => '-- SELECIONE --',
                'required' => true
            ))
            ->add('instMunTotalInstituicoesFiscalizadasMunicipio', null, array(
                'label' => 'app.relfiscinstituicoesmunicipio.instmuntotalinstituicoesfiscalizadasmunicipio',
                'translation_domain' => 'EntityTranslations',
                'horizontal' => false,
                'widget_form_group_attr' => array(
                    'class' => 'col-md-5'
                ),
                'required' => true
            ))
//            ->add('procTrabInstituicoes', null, array(
//                'label' => 'app.relfiscinstituicoesmunicipio.proctrabinstituicoes',
//                'translation_domain' => 'EntityTranslations'
//            ))

        ;
    }


    /**
     * @param OptionsResolver $resolver
     */
    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => RelFiscInstituicoesMunicipio::class,
            'municipios' => array(),
            'instancia' => null
        ));
    }
}

RelFiscRelatorio.php

<?php

namespace AdminRelatorioFiscalizacaoBundleEntity;

use DoctrineORMMapping as ORM;

/**
 * RelFiscRelatorio
 *
 * @ORMTable(name="rel_fisc_relatorio")
 * @ORMEntity(repositoryClass="AdminRelatorioFiscalizacaoBundleRepositoryRelFiscRelatorioRepository")
 */
class RelFiscRelatorio
{
    /**
     * @var int
     *
     * @ORMColumn(name="id", type="integer")
     * @ORMId
     * @ORMGeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var RelFiscIdentificacao
     *
     * @ORMManyToOne(targetEntity="AdminRelatorioFiscalizacaoBundleEntityRelFiscIdentificacao",cascade={"PERSIST"})
     * @ORMJoinColumns({
     *   @ORMJoinColumn(name="identificacao_id", referencedColumnName="id", nullable=true)
     * })
     */
    private $identificacao;

    /**
     * @var RelFiscRecursosHumanos
     *
     * @ORMManyToOne(targetEntity="AdminRelatorioFiscalizacaoBundleEntityRelFiscRecursosHumanos",cascade={"PERSIST"})
     * @ORMJoinColumns({
     *   @ORMJoinColumn(name="recursos_humanos_id", referencedColumnName="id", nullable=true)
     * })
     */
    private $recursosHumanos;

    /**
     * @var RelFiscInfraestrutura
     *
     * @ORMManyToOne(targetEntity="AdminRelatorioFiscalizacaoBundleEntityRelFiscInfraestrutura",cascade={"PERSIST"})
     * @ORMJoinColumns({
     *   @ORMJoinColumn(name="infraestrutura_id", referencedColumnName="id", nullable=true)
     * })
     */
    private $infraestrutura;

    /**
     * @var RelFiscProcessosDeTrabalho
     *
     * @ORMManyToOne(targetEntity="AdminRelatorioFiscalizacaoBundleEntityRelFiscProcessosDeTrabalho",cascade={"PERSIST"})
     * @ORMJoinColumns({
     *   @ORMJoinColumn(name="processos_de_trabalho_id", referencedColumnName="id", nullable=true)
     * })
     */
    private $processosDeTrabalho;

    /**
     * @var RelFiscProcessosFiscalizacaoAcompanhamentoAutuacoes
     *
     * @ORMManyToOne(targetEntity="AdminRelatorioFiscalizacaoBundleEntityRelFiscProcessosFiscalizacaoAcompanhamentoAutuacoes",cascade={"PERSIST"})
     * @ORMJoinColumns({
     *   @ORMJoinColumn(name="processos_fiscalizacao_acompanhamento_autuacoes_id", referencedColumnName="id", nullable=true)
     * })
     */
    private $processosFiscalizacaoAcompanhamentoAutuacoes;

    /**
     * @var string
     *
     * @ORMColumn(name="observacoes_gerais", type="text", nullable=true)
     */
    private $observacoesGerais;

    /**
     * @var int
     *
     * @ORMColumn(name="ano", type="integer")
     */
    private $ano;

    /**
     * @var int
     *
     * @ORMColumn(name="trimestre", type="integer")
     */
    private $trimestre;

    /**
     * @var DateTime
     *
     * @ORMColumn(name="data", type="datetime")
     */
    private $data;

    /**
     * @var bool
     *
     * @ORMColumn(name="ativo", type="boolean")
     */
    private $ativo;

    /**
     * @var User
     *
     * @ORMManyToOne(targetEntity="CoreBundleUserBundleEntityUser")
     * @ORMJoinColumns({
     *   @ORMJoinColumn(name="user_id", referencedColumnName="id")
     * })
     */
    private $user;

    /**
     * @var CoreBundleInstanciaBundleEntityInstancia
     *
     * @ORMManyToOne(targetEntity="CoreBundleInstanciaBundleEntityInstancia")
     * @ORMJoinColumns({
     *   @ORMJoinColumn(name="instancia_id", referencedColumnName="id")
     * })
     */
    private $instancia;

    /**
     * @var CoreBundleInstanciaBundleEntityInstancia
     *
     * @ORMOneToMany(targetEntity="AdminRelatorioFiscalizacaoBundleEntityRelFiscHistorico", mappedBy="relatorio", cascade={"PERSIST"})
     */
    private $historicos;

    public function __construct()
    {
        $this->data = new DateTime();
    }


    /**
     * Get id.
     *
     * @return int
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * @return RelFiscIdentificacao
     */
    public function getIdentificacao()
    {
        return $this->identificacao;
    }

    /**
     * @param RelFiscIdentificacao $identificacao
     */
    public function setIdentificacao($identificacao)
    {
        $this->identificacao = $identificacao;
        return $this;
    }

    /**
     * @return RelFiscRecursosHumanos
     */
    public function getRecursosHumanos()
    {
        return $this->recursosHumanos;
    }

    /**
     * @param RelFiscRecursosHumanos $recursosHumanos
     */
    public function setRecursosHumanos($recursosHumanos)
    {
        $this->recursosHumanos = $recursosHumanos;
        return $this;
    }

    /**
     * @return RelFiscInfraestrutura
     */
    public function getInfraestrutura()
    {
        return $this->infraestrutura;
    }

    /**
     * @param RelFiscInfraestrutura $infraestrutura
     */
    public function setInfraestrutura($infraestrutura)
    {
        $this->infraestrutura = $infraestrutura;
        return $this;
    }

    /**
     * @return RelFiscProcessosDeTrabalho
     */
    public function getProcessosDeTrabalho()
    {
        return $this->processosDeTrabalho;
    }

    /**
     * @param RelFiscProcessosDeTrabalho $processosDeTrabalho
     */
    public function setProcessosDeTrabalho($processosDeTrabalho)
    {
        $this->processosDeTrabalho = $processosDeTrabalho;
        return $this;
    }

    /**
     * @return RelFiscProcessosFiscalizacaoAcompanhamentoAutuacoes
     */
    public function getProcessosFiscalizacaoAcompanhamentoAutuacoes()
    {
        return $this->processosFiscalizacaoAcompanhamentoAutuacoes;
    }

    /**
     * @param RelFiscProcessosFiscalizacaoAcompanhamentoAutuacoes $processosFiscalizacaoAcompanhamentoAutuacoes
     */
    public function setProcessosFiscalizacaoAcompanhamentoAutuacoes($processosFiscalizacaoAcompanhamentoAutuacoes)
    {
        $this->processosFiscalizacaoAcompanhamentoAutuacoes = $processosFiscalizacaoAcompanhamentoAutuacoes;
        return $this;
    }

    /**
     * @return string
     */
    public function getObservacoesGerais()
    {
        return $this->observacoesGerais;
    }

    /**
     * @param string $observacoesGerais
     */
    public function setObservacoesGerais($observacoesGerais)
    {
        $this->observacoesGerais = $observacoesGerais;
        return $this;
    }

    /**
     * @return int
     */
    public function getAno()
    {
        return $this->ano;
    }

    /**
     * @param int $ano
     */
    public function setAno($ano)
    {
        $this->ano = $ano;
        return $this;
    }

    /**
     * @return int
     */
    public function getTrimestre()
    {
        return $this->trimestre;
    }

    /**
     * @param int $trimestre
     */
    public function setTrimestre($trimestre)
    {
        $this->trimestre = $trimestre;
        return $this;
    }

    /**
     * @return DateTime
     */
    public function getData()
    {
        return $this->data;
    }

    /**
     * @param DateTime $data
     */
    public function setData($data)
    {
        $this->data = $data;
        return $this;
    }

    /**
     * @return bool
     */
    public function isAtivo()
    {
        return $this->ativo;
    }

    /**
     * @param bool $ativo
     */
    public function setAtivo($ativo)
    {
        $this->ativo = $ativo;
        return $this;
    }

    /**
     * @return User
     */
    public function getUser()
    {
        return $this->user;
    }

    /**
     * @param User $user
     */
    public function setUser($user)
    {
        $this->user = $user;
        return $this;
    }

    /**
     * @return CoreBundleInstanciaBundleEntityInstancia
     */
    public function getInstancia()
    {
        return $this->instancia;
    }

    /**
     * @param CoreBundleInstanciaBundleEntityInstancia $instancia
     */
    public function setInstancia($instancia)
    {
        $this->instancia = $instancia;
        return $this;
    }

    /**
     * @return CoreBundleInstanciaBundleEntityInstancia
     */
    public function getHistoricos()
    {
        return $this->historicos;
    }

    /**
     * @param CoreBundleInstanciaBundleEntityInstancia $historicos
     */
    public function setHistoricos($historicos)
    {
        $this->historicos = $historicos;
        return $this;
    }
}

RelFiscRelatorioController.php

...
$processos_trabalho = new RelFiscProcessosDeTrabalho();
if ($form->isSubmitted() && $form->isValid()) {
    foreach ($processos_trabalho->getInstTotalMunicipios() as $instMunicipioTotal) {
        $instMunicipios = new RelFiscInstituicoesMunicipio();
        $instMunicipios
            ->setInstMunTotalInstituicoesFiscalizadasMunicipio($instMunicipioTotal->getInstMunTotalInstituicoesFiscalizadasMunicipio())
            ->setMunicipio($instMunicipioTotal->getMunicipio())
            ->setProcTrabInstituicoes($processos_trabalho);

        $processos_trabalho->addInstTotalMunicipio($instMunicipios);
        $em->persist($instMunicipios);
    }
}

When I work with the RelFiscProcessosDeTrabalho class individually, using foreach after submitting and validating the form of the class itself, the data is saved normally. However, when I use the same method in the RelFiscRelatorioController, no data is saved for the RelFiscProcessosDeTrabalho class, but the data for the RelFiscRelatorio.php class is saved completely without any problem.

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

Your email address will not be published. Required fields are marked *