How to insert an object in doctrine, without persisting in other tables?

0

I am trying to insert a record into a table that contains compound key. This is the error:

  

"A new entity was found through the relationship   'EnFOperEstadoBean # grupoCompanica' that was not configured to cascade   persist operations for entity:   EnGroupBean @ 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 can not find out   which entity causes the problem to implement   'EnFGroupBean #__ toString ()' to get a clue. "

Well, if I put cascade={"persist"} , it tries to create record in other tables. Do you have any option for him to do nothing? I tried the detach to remove the relationships with other tables, however it 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);
        }
    }
    
asked by anonymous 03.12.2015 / 18:15

1 answer

0

The $this->GrupoEmpresaBO->findByPk($grupoEmpresa) method is probably 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 object already persisted in the findByPk method, even if it is a new object.

    
08.12.2015 / 20:23