/** * Create a Voucher Group * * @param VoucherGroup $oVoucherGroup The Voucher Group Entity * @throws VoucherException if the database query fails or entity has id assigned. * @returns boolean true if the insert operation was successful */ public function execute(VoucherGroup $oVoucherGroup) { $oGateway = $this->oGateway; $oVoucherBuilder = $oGateway->getEntityBuilder(); $bSuccess = false; if (false === empty($oVoucherGroup->getVoucherGroupId())) { throw new VoucherException('Unable to create new voucher group the Entity has a database id assigned already'); } try { $oQuery = $oGateway->insertQuery()->start(); foreach ($oVoucherBuilder->demolish($oVoucherGroup) as $sColumn => $mValue) { if ($sColumn !== 'voucher_group_id' && $sColumn !== 'date_created') { $oQuery->addColumn($sColumn, $mValue); } elseif ($sColumn === 'date_created') { $oQuery->addColumn('date_created', $this->oNow); } } $bSuccess = $oQuery->end()->insert(); if ($bSuccess) { $oVoucherGroup->setVoucherGroupId($oGateway->lastInsertId()); } } catch (DBALGatewayException $e) { throw new VoucherException($e->getMessage(), 0, $e); } return $bSuccess; }
/** * Remove a Voucher Group * * @param VoucherGroup $oVoucherGroup The Voucher Group Entity * @throws VoucherException if the database query fails or entity has id assigned. * @returns boolean true if the insert operation was successful */ public function execute(VoucherGroup $oVoucherGroup) { $oGateway = $this->oGateway; $oVoucherBuilder = $oGateway->getEntityBuilder(); $bSuccess = false; if (true === empty($oVoucherGroup->getVoucherGroupId())) { throw new VoucherException('Unable to remove a voucher group the Entity does not have a database id'); } try { // Note: the FK will stop voucher groups from being removed if they are used. $bSuccess = $oGateway->deleteQuery()->start()->filterByGroup($oVoucherGroup->getVoucherGroupId())->end()->delete(); if (!$bSuccess) { throw new VoucherException('Unable to remove voucher at id::' . $oVoucherGroup->getVoucherGroupID()); } } catch (DBALGatewayException $e) { throw new VoucherException($e->getMessage(), 0, $e); } return $bSuccess; }
/** * Create a Voucher Group * * @param VoucherGroup $oVoucherGroup The Voucher Group Entity * @throws VoucherException if the database query fails or entity has id assigned. * @returns boolean true if the insert operation was successful */ public function execute(VoucherGroup $oVoucherGroup) { $oGateway = $this->oGateway; $oVoucherBuilder = $oGateway->getEntityBuilder(); if (true === empty($oVoucherGroup->getVoucherGroupId())) { throw new VoucherException('Unable to save voucher group the Entity has no database id assigned'); } try { $oQuery = $oGateway->updateQuery()->start(); foreach ($oVoucherBuilder->demolish($oVoucherGroup) as $sColumn => $mValue) { if ($sColumn !== 'voucher_group_id' && $sColumn !== 'date_created') { $oQuery->addColumn($sColumn, $mValue); } } $bSuccess = $oQuery->where()->filterByGroup($oVoucherGroup->getVoucherGroupId())->end()->update(); if ($bSuccess) { $oVoucherGroup->setVoucherGroupID($oGateway->lastInsertId()); } } catch (DBALGatewayException $e) { throw new VoucherException($e->getMessage(), 0, $e); } return $bSuccess; }
/** * Convert and entity into a data array that match database columns in table * * @return array * @access public * @param VoucherGroup $entity A voucher group entity */ public function demolish($entity) { $aData = array('voucher_group_id' => $entity->getVoucherGroupId(), 'voucher_group_name' => $entity->getVoucherGroupName(), 'voucher_group_slug' => $entity->getSlugName(), 'is_disabled' => $entity->getDisabledStatus(), 'sort_order' => $entity->getSortOrder(), 'date_created' => $entity->getDateCreated()); return $aData; }