How to insert an object in Doctrine without persisting in other tables?

Asked

Viewed 148 times

1

I’m trying to insert a record into a table that contains composite key. That is the mistake:

"A new Entity was found through the Relationship 'Enfoperestadobean#grupoEmpresa' that was not configured to Cascade persist Operations for Entity: Enfgrupoempresabean@000000000183abf100000000178339a6. To Solve this Issue: Either explicitly call Entitymanager#persist() on this Unknown Entity or configure Cascade persist this Association in the Mapping for example @Manytoone(..,Cascade={"persist"}). If you cannot find out which Entity causes the problem implement 'Enfgrupoempresabean#__toString()' to get a Clue."

Well, if I put the cascade={"persist"}, he tries to create record in other tables. Do you have an option for him to do nothing? I tried the detach to remove the relationships with other tables but gives error again.

To the codes...

Insert

  public function insert($grupoEmpresa, $ckDentroForaOperacao, $pcReduzBaseCalcOperacao, $ckCalculaIcms,            
    $dsObsOperacao, $vlSituTribOperacao, $operacao, $estado, $cdEnquadramentoIpi)
    {
        try{
            $this->CON->beginTransaction();

            $bean = $this->populaBean($grupoEmpresa, $ckDentroForaOperacao, $pcReduzBaseCalcOperacao, $ckCalculaIcms,            
            $dsObsOperacao, $vlSituTribOperacao, $operacao, $estado, $cdEnquadramentoIpi);

            $this->DAO->insert($this->CON,$bean);
            $this->CON->commit();
            $this->CON->flush();

        } catch (Exception $ex) {
            $this->CON->rollback();
            throw new BusinessArgusException("Problemas em inserir objeto", $ex);
        }
    }

    public function populaBean($grupoEmpresa, $ckDentroForaOperacao, $pcReduzBaseCalcOperacao, $ckCalculaIcms,            
    $dsObsOperacao, $vlSituTribOperacao, $operacao, $estado, $cdEnquadramentoIpi)
    {
        try{

//Ao inserir o persist em todos os campos que fazem relação com outras tabelas, conforme sugerido no erro, o erro muda, e vira este abaixo.

//"An exception occurred while executing 'INSERT INTO SEASON_MIC.ENF_ESTADO (CD_ESTADO, NM_ESTADO, CD_ESTADO_IBGE, CD_TIPO_ESTADO, CK_CONTINGENCIA, CK_HORARIO_VERAO, GMT, QT_HORAS_PRAZO_CANCELAMENTO, VL_ALIQUOTA_ICMS_INTERNA, VL_PORCENTAGEM_MANDA_POBRE, VL_PORCENTAGEM_MANDA_RICO) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)' with params {"1":"SC","2":"SANTA CATARINA","3":"42","4":"R","5":"N","6":null,"7":null,"8":24,"9":"17","10":7,"11":12}:  ORA-00001: restrição exclusiva (SEASON_MIC.PK_ENF_ESTADO


    $bean = new EnfOperEstadoBean();

    $bean->setGrupoEmpresa($this->GrupoEmpresaBO->findByPk($grupoEmpresa));

    $bean->setCkDentroForaOperacao($ckDentroForaOperacao);
    $bean->setPcReduzBaseCalcOperacao($pcReduzBaseCalcOperacao);
    $bean->setCkCalculaIcms($ckCalculaIcms);
    $bean->setDsObsOperacao($dsObsOperacao);
    $bean->setVlSituTribOperacao($vlSituTribOperacao);

    $bean->setOperacao($this->OperacaoBO->findByPk($operacao));

    $bean->setEstado($this->EstadoBO->findByPk($estado)); 

    $bean->setCdEnquadramentoIpi($cdEnquadramentoIpi);

    return $bean;

    } catch (Exception $ex) {
            throw new BusinessArgusException("Problemas ao popular o objeto", $ex);
        }
    }

1 answer

0

Probably the method $this->GrupoEmpresaBO->findByPk($grupoEmpresa) is returning a new company that is not persisted in the database when you try to persist the object of type EnfOperEstadoBean.

You need to persist the first object and then persist the second:

$grupoEmpresa = $this->GrupoEmpresaBO->findByPk($grupoEmpresa);
$bean->setGrupoEmpresa($grupoEmpresa);

$this->entityManager->persist($grupoEmpresa);
$this->entityManager->persist($bean);
$this->entityManager->flush();

Another option is to return an already persisted object in the method findByPk, even if it is a new object.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.