/** * Create a Voucher Instance * * @param VoucherInstance $oVoucherGroup The Voucher Instance to save * @throws VoucherException if the database query fails or entity has id assigned. * @returns boolean true if the insert operation was successful */ public function execute(VoucherInstance $oVoucherInstance) { $oGateway = $this->oGateway; $oBuilder = $oGateway->getEntityBuilder(); if (false === empty($oVoucherInstance->getVoucherInstanceId())) { throw new VoucherException('Unable to create new voucher instance the Entity has a database id assigned already'); } try { $oQuery = $oGateway->insertQuery()->start(); foreach ($oBuilder->demolish($oVoucherInstance) as $sColumn => $mValue) { if ($sColumn !== 'voucher_instance_id' && $sColumn !== 'date_created') { $oQuery->addColumn($sColumn, $mValue); } elseif ($sColumn === 'date_created') { $oQuery->addColumn('date_created', $this->oNow); } } $bSuccess = $oQuery->end()->insert(); if ($success) { $oVoucherInstance->setVoucherInstanceId($gateway->lastInsertId()); } } catch (DBALGatewayException $e) { throw new VoucherException($e->getMessage(), 0, $e); } return $success; }
/** * Convert data array into entity * * @return VoucherGroup * @param array $data * @access public */ public function build($data) { $oEntity = new VoucherInstance(); $sAlias = $this->getTableQueryAlias(); $iVoucherInstanceId = $this->getField($data, 'voucher_instance_id', $sAlias); $iVoucherTypeId = $this->getField($data, 'voucher_type_id', $sAlias); $sVoucherCode = $this->getField($data, 'voucher_code', $sAlias); $oDateCreated = $this->getField($data, 'date_created', $sAlias); $oEntity->setVoucherInstanceId($iVoucherInstanceId); $oEntity->setVoucherTypeId($iVoucherTypeId); $oEntity->setVoucherCode($sVoucherCode); $oEntity->setDateCreated($oDateCreated); return $oEntity; }
public function testVoucherInstance() { $oInstance = new VoucherInstance(); $iVoucherInstanceId = 1; $iVoucherTypeId = 1; $sVoucherCode = '00_111_00'; $oDateCreated = new DateTime(); $oInstance->setVoucherInstanceId($iVoucherInstanceId); $oInstance->setVoucherTypeId($iVoucherTypeId); $oInstance->setVoucherCode($sVoucherCode); $oInstance->setDateCreated($oDateCreated); $this->assertEquals($iVoucherInstanceId, $oInstance->getVoucherInstanceId()); $this->assertEquals($iVoucherTypeId, $oInstance->getVoucherTypeId()); $this->assertEquals($sVoucherCode, $oInstance->getVoucherCode()); $this->assertEquals($oDateCreated, $oInstance->getDateCreated()); }