public function insert(array $data) { if (isset($data['acc_no'])) { if (strchr($data['acc_no'], '-')) { $range = explode('-', $data['acc_no']); if (2 == count($range) and (string) $range[0] === (string) (int) $range[0] and (string) $range[1] === (string) (int) $range[1] and $range[0] <= $range[1]) { $cols = array(); $vals = array(); foreach ($data as $col => $val) { $cols[] = $this->getAdapter()->quoteIdentifier($col, true); for ($acc = $range[0]; $acc <= $range[1]; $acc++) { if ('acc_no' == $col) { $vals[$acc][] = $this->getAdapter()->quote($acc); } else { $vals[$acc][] = $val ? $this->getAdapter()->quote($val) : 'null'; } } } //build values statement $tmpVals = array(); foreach ($vals as $acc => $values) { $tmpVals[] = ' (' . implode(', ', $values) . ') '; } // build the statement $sql = "INSERT INTO " . $this->getAdapter()->quoteIdentifier(self::TABLE_NAME, true) . ' (' . implode(', ', $cols) . ') ' . 'VALUES ' . implode(', ', $tmpVals); // execute the statement and return the number of affected rows $stmt = $this->getAdapter()->query($sql); $result = $stmt->rowCount(); return $result; } else { throw new Zend_Db_Table_Exception('The range ' . $range[0] . ' - ' . $range[1] . ' is not acceptable.'); } } elseif ((string) $data['acc_no'] === (string) (int) $data['acc_no']) { return parent::insert($data); } else { throw new Zend_Db_Table_Exception("acc_no is not acceptable."); } } else { if ($this->debug) { throw new Zend_Db_Table_Exception('Books cannot be added without acc_no!!'); } } }
/** * (C)reate, (R)ead, (U)pdate, (D)elete function. * * All CRUD activities pass through this method. * @return mixed Status of Add/Update/Delete commands if successful else exception string with header status {@link $errorCode}. */ public function crudAction() { $request = $this->getRequest(); $status = NULL; $errorCode = 400; if ($request->isXmlHttpRequest() and $request->isPost()) { self::createDbCols(); switch ($request->getParam('oper')) { case 'add': $rdata = $request->getParams(); $data = self::paramData($rdata); try { $status = $this->model->insert($data); echo $status; } catch (Zend_Exception $e) { $this->_helper->logger->err($e->getMessage()); $this->_helper->logger->alert('Row insertion failure :'); $this->_helper->logger('Status = ' . $status); $this->_helper->logger->alert('Request Data :'); $this->_helper->logger($rdata); $this->_helper->logger->alert('Processed data :'); $this->_helper->logger($data); throw $e; } if ($status) { $this->_helper->logger->debug('Status :'); $this->_helper->logger($status); } return true; break; case 'del': $id = $request->getParam('id'); $where = self::whereData($id); try { $status = $this->model->delete($where); } catch (Zend_Exception $e) { $this->_helper->logger->err($e->getMessage()); $this->_helper->logger->err('Row deletion failure :'); $this->_helper->logger('Status = ' . $status); $this->_helper->logger->alert('WHERE clause :'); $this->_helper->logger($where); $this->getResponse()->setHttpResponseCode($errorCode); throw $e; } if ($status) { $this->_helper->logger->debug('Status :'); $this->_helper->logger($status); } return true; break; case 'edit': $rdata = $request->getParams(); $data = self::paramData($rdata); $id = $rdata['id']; $where = self::whereData($id); try { $status = $this->model->update($data, $where); } catch (Zend_Exception $e) { $this->_helper->logger->err($e->getMessage()); $this->_helper->logger->err('Row updation failure :'); $this->_helper->logger('Status = ' . $status); $this->getResponse()->setHttpResponseCode($errorCode); throw $e; } if ($status) { $this->_helper->logger->debug('Status :'); $this->_helper->logger($status); } return true; break; default: $this->_helper->logger->crit('The param "oper" has unexpected value :' . $request->getParam('oper')); return false; } } else { $this->_helper->logger->err('Criteria to access CRUD is not fullfilled'); header("HTTP/1.1 403 Forbidden"); die; } }