/** * @param \Empresa\Entity\Empresa * @return ResultInterface */ public function save(EmpresaEntity $empresa) { $db = $this->getDbAdapter(); $con = $db->getDriver()->getConnection(); $con->beginTransaction(); try { if (!$empresa->getId()) { $arrUser = $this->usuarioMapper->getHydrator()->extract($empresa->getUsuario()); unset($arrUser['id']); $arrEnd = $this->enderecoMapper->getHydrator()->extract($empresa->getEndereco()); unset($arrEnd['id']); $empresa->setUsrId($this->usuarioMapper->insert($arrUser)->getGeneratedValue())->setEnderecoId($this->enderecoMapper->insert($arrEnd)->getGeneratedValue()); parent::insert($empresa); } else { //entitys to array $arrayUser = $this->usuarioMapper->getHydrator()->extract($empresa->getUsuario()); $usr_id = $arrayUser['id']; unset($arrayUser['id']); $arrayEnd = $this->enderecoMapper->getHydrator()->extract($empresa->getEndereco()); $end_id = $arrayEnd['id']; unset($arrayEnd['id']); $this->usuarioMapper->update($arrayUser, sprintf("usr_id = %s", $usr_id)); $this->enderecoMapper->update($arrayEnd, sprintf("end_id = %s", $end_id)); $empresa->setUsrId($empresa->getUsuario()->getId())->setEnderecoId($empresa->getEndereco()->getId()); $arrayEmp = $this->getHydrator()->extract($empresa); $emp_id = $arrayEmp['id']; unset($arrayEmp['id']); parent::update($arrayEmp, sprintf("id = %s", $emp_id)); } $con->commit(); } catch (Exception $e) { $con->rollback(); throw $e; } }