/** * This method init $this->object */ protected function _initObject($data = null) { if (is_null($data)) { $data = $this->_itemsArrayDelault; } $this->_prepareTable($data); $this->dbTable->insert($data); }
/** * {@inheritDoc} */ protected function doUpdateModel($model) { if ($id = $this->getModelId($model)) { $this->tableGateway->update($this->tableGateway->getResultSetPrototype()->getHydrator()->extract($model), array("{$this->idField} = ?" => $id)); } else { $this->tableGateway->insert($this->tableGateway->getResultSetPrototype()->getHydrator()->extract($model)); } }
/** * @param Request $request * @param Response $response * @param $args * * @return ResponseInterface */ public function create(Request $request, Response $response, $args) { try { $this->gateway->insert($request->getParsedBody()); return $response->withJson(["result" => $this->gateway->lastInsertValue]); } catch (\Exception $e) { return $response->withStatus(400, $e->getMessage()); } }
/** * Create a resource * * @param mixed $data * @return ApiProblem|mixed */ public function create($data) { $entity = new ClienteEntity(); $data = json_decode(json_encode($data), true); $entity->exchangeArray($data); $data = $entity->getArrayCopy(); $this->tableGateway->insert($data); $data['id'] = $this->tableGateway->getLastInsertValue(); return $data; }
/** * @param array $data * @return object */ public function create(array $data) { $this->table->insert($data); $id = $this->table->getLastInsertValue(); $entity = $this->find($id); if (!$entity) { throw new \Exception("Entity not created?"); } return $entity; }
/** * @param string $key * @param DateTime $endDate * @return mixed Lock\Handle or false */ public function setLock($key, DateTime $endDate) { try { $result = $this->gateway->insert(['key' => $key, 'end_datetime' => $endDate->format($this->options->getDbDateTimeFormat())]); if ($result) { return $this->createLockHandle($key); } } catch (\Exception $e) { } return false; }
/** * @param AbstractModel $model */ protected function internalSave(AbstractModel $model) { $pKey = $this->getPrimaryKey(); if (isset($model->id)) { $this->tableGateway->update($model->toArray(), [$pKey => $model->{$pKey}]); } else { $this->tableGateway->insert($model->toArray()); $id = $this->tableGateway->getLastInsertValue(); $model->{$pKey} = $id; } }
/** * @param AbstractEntity $entity * @return $this */ public function persist(AbstractEntity $entity) { $data = $this->hydrator->extract($entity); if ($this->hasIdentity($entity)) { $this->gateway->update($data, ['id' => $entity->getId()]); } else { $this->gateway->insert($data); $entity->setId($this->gateway->getLastInsertValue()); } return $this; }
public function saveCategory(Category $category) { $data = array('id_category' => $category->id_category, 'name' => $category->name, 'slug' => $category->slug); $id = (int) $category->id_category; if ($id == 0) { $this->tableGateway->insert($data); } elseif ($this->getCategoryById($id)) { $this->tableGateway->update($data, array('id_category = ?' => $id)); } else { throw new \Exception('Form id does not exist'); } }
/** * Saves an account to the db. If the id exists the given dataset will be updated * @param \Account\Model\Account $account * * @throws \Exception */ public function saveWarlog(Warlog $warlog) { $data = ['wins' => $warlog->getWins(), 'losses' => $warlog->getLosses(), 'draws' => $warlog->getDraws()]; if (!$warlog->getId()) { $this->tableGateway->insert($data); } else { if ($this->getWarlog()) { $this->tableGateway->update($data, ['id' => $warlog->getId()]); } else { throw new \Exception('Account id does not exist'); } } }
/** * Saves the given NewsCategory object to the db * * @param NewsCategory * @throws \Exception */ public function saveNewsCategory(NewsCategory $nc) { $data = ['name' => $nc->getName()]; $id = (int) $nc->getId(); if ($id == 0) { $this->tableGateway->insert($data); } else { if ($this->getNewsCategoryBy(['id' => $id])) { $this->tableGateway->update($data, ['id' => $id]); } else { throw new \Ȩxception('NewsCategory id does not exist'); } } }
/** * Saves the given object ot the db * @param Warstatus $warstatus * @throws \Exception */ public function saveWarstatus(Warstatus $warstatus) { $data = $warstatus->getArrayCopy(); if ($warstatus->getId() == 0) { throw new \Exception('Id needed to save a warstatus'); } else { if ($this->getWarstatus((int) $warstatus->getId())) { $this->tableGateway->update($data, ['id' => (int) $warstatus->getId()]); } else { $data['id'] = $warstatus->getId(); $this->tableGateway->insert($data); } } }
public function saveForm(Form $form) { $data = array('name' => $form->name); $id = (int) $form->id; if ($id == 0) { $this->tableGateway->insert($data); } else { if ($this->getForm($id)) { $this->tableGateway->update($data, array('id' => $id)); } else { throw new \Exception('Form id does not exist'); } } }
/** * Saves the given news object to the db * * @param \News\Model\News $news * * @throws \Exception */ public function saveNews(News $news) { $data = ['account_id' => $news->getAccountId(), 'title' => $news->getTitle(), 'content' => $news->getContent(), 'category_id' => $news->getCategoryId(), 'date_posted' => $news->getDatePosted()]; $id = (int) $news->getId(); if ($id == 0) { $this->tableGateway->insert($data); } else { if ($this->getNews($id)) { $this->tableGateway->update($data, ['id' => $id]); } else { throw new \Exception('News id does not exist'); } } }
/** * Saves the given media object to the db * * @param \Media\Model\Media $media * * @throws \Exception */ public function saveMedia(Media $media) { $data = ['account_id' => $media->getAccountId(), 'title' => $media->getTitle(), 'url' => $media->getUrl(), 'date_posted' => $media->getDatePosted()]; $id = (int) $media->getId(); if ($id == 0) { $this->tableGateway->insert($data); } else { if ($this->getMedia($id)) { $this->tableGateway->update($data, ['id' => $id]); } else { throw new \Exception('Media id does not exist'); } } }
public function saveCore(Core $core) { $data = array('name' => $core->name); $id = (int) $core->id; if ($id == 0) { $this->tableGateway->insert($data); } else { if ($this->getCore($id)) { $data['last_updated'] = $core->lastUpdated; $this->tableGateway->update($data, array('id' => $id)); } else { throw new \Exception('Core id does not exist'); } } }
/** * * @throws \Exception * @return number */ public function save($entity, $where = null) { // if $entity do not exists, return false if ($entity == false) { return false; } $table = $this->getTableGateway()->table; $columns = $this->getColumns(); foreach ($columns as $key => $value) { if (property_exists($entity, $value)) { $data[$value] = $entity->{$value}; } } // if id is set then also we need to update // create $where based on id in such a case try { if (!is_array($where)) { if ($data['id'] > 0) { $where = array("id" => $data['id']); $this->tableGateway->update($data, $where); return $data['id']; } else { // insert and return last ID $this->tableGateway->insert($data); return $this->tableGateway->lastInsertValue; } } else { $this->tableGateway->update($data, $where); return $data['id']; } } catch (\Exception $e) { die($e->getMessage()); } }
public function saveCommandeProduit($id, $ajouterCommande, $prixproduit) { $data = array('COMMANDE_ID' => $id, 'PRODUIT_ID' => $ajouterCommande, 'COMMANDEPRODUIT_PRIX' => $prixproduit); $adapter = $this->tableGateway->getAdapter(); $otherTable = new TableGateway('commandeproduit', $adapter); $otherTable->insert($data); }
public function insertanywhere($mytable, array $data) { $db = $this->getServiceLocator()->get('Zend\\Db\\Adapter\\Adapter'); $table = new TableGateway($mytable, $db); $results = $table->insert($data); return 1; }
protected function _update($itemData, $createIfAbsent = false) { $adapter = $this->dbTable->getAdapter(); $identifier = $this->getIdentifier(); if (!isset($itemData[$identifier])) { throw new DataStoreException('Item must has primary key'); } $id = $itemData[$identifier]; $this->checkIdentifierType($id); $queryStr = 'SELECT ' . Select::SQL_STAR . ' FROM ' . $adapter->platform->quoteIdentifier($this->dbTable->getTable()) . ' WHERE ' . $adapter->platform->quoteIdentifier($identifier) . ' = ?' . ' FOR UPDATE'; //is row with this index exist? $rowset = $adapter->query($queryStr, array($id)); $isExist = !is_null($rowset->current()); $result = []; switch (true) { case !$isExist && !$createIfAbsent: throw new DataStoreException('Can\'t update item with "id" = ' . $id); case !$isExist && $createIfAbsent: $this->dbTable->insert($itemData); $result = $itemData; break; case $isExist: unset($itemData[$identifier]); $this->dbTable->update($itemData, array($identifier => $id)); $rowset = $adapter->query($queryStr, array($id)); $result = $rowset->current()->getArrayCopy(); break; } return $result; }
/** * Save Role * * @return integer */ public function save() { $this->events()->trigger(__CLASS__, 'before.save', $this); $arraySave = array('name' => $this->getName(), 'description' => $this->getDescription()); try { $roleId = $this->getId(); if (empty($roleId)) { $this->insert($arraySave); $this->setId($this->getLastInsertId()); } else { $this->update($arraySave, array('id' => $this->getId())); } $permissions = $this->getPermissions(); if (!empty($permissions)) { $aclTable = new TableGateway('user_acl', $this->getAdapter()); $aclTable->delete(array('user_acl_role_id' => $this->getId())); foreach ($permissions as $permissionId => $value) { if (!empty($value)) { $aclTable->insert(array('user_acl_role_id' => $this->getId(), 'user_acl_permission_id' => $permissionId)); } } } $this->events()->trigger(__CLASS__, 'after.save', $this); return $this->getId(); } catch (\Exception $e) { $this->events()->trigger(__CLASS__, 'after.save.failed', $this); throw new \Gc\Exception($e->getMessage(), $e->getCode(), $e); } }
/** * Insert node * * @param integer $level * @param integer $nearKey * @param array $data * @param array $filter * @param boolean $useTransaction * @return integer|string */ protected function insertNode($level, $nearKey, array $data, array $filter = [], $useTransaction = true) { try { if ($useTransaction) { $this->tableGateway->getAdapter()->getDriver()->getConnection()->beginTransaction(); } $this->tableGateway->update([$this->left => new Expression('IF (' . $this->left . ' > ?, ' . $this->left . ' + 2, ' . $this->left . ')', [$nearKey]), $this->right => new Expression('IF (' . $this->right . ' > ?, ' . $this->right . ' + 2, ' . $this->right . ')', [$nearKey])], [(new Predicate())->greaterThanOrEqualTo($this->right, $nearKey)] + $filter); $leftKey = $nearKey + 1; $rightKey = $nearKey + 2; $level = $level + 1; // insert a new node $this->tableGateway->insert([$this->left => $leftKey, $this->right => $rightKey, $this->level => $level] + $data); $insertId = $this->tableGateway->getLastInsertValue(); // update parent info if (false !== ($parent = $this->getParentNode($leftKey, $rightKey, $level, $filter))) { $this->tableGateway->update([$this->parent => $parent[$this->nodeId]], [$this->nodeId => $insertId]); } if ($useTransaction) { $this->tableGateway->getAdapter()->getDriver()->getConnection()->commit(); } } catch (Exception $e) { if ($useTransaction) { $this->tableGateway->getAdapter()->getDriver()->getConnection()->rollback(); } ApplicationErrorLogger::log($e); return $e->getMessage(); } return $insertId; }
public function insertCollection($data) { parent::insert($data); $adapter = $this->getAdapter(); $id = $adapter->getDriver()->getLastGeneratedValue(); return $id; }
/** * @covers Zend\Db\TableGateway\TableGateway::__get */ public function test__get() { $this->table->insert(array('foo')); // trigger last insert id update $this->assertEquals(10, $this->table->lastInsertValue); $this->assertSame($this->mockAdapter, $this->table->adapter); $this->assertEquals('foo', $this->table->table); }
public function lastInsertId($mytable, array $data) { $db = $this->getServiceLocator()->get('Zend\\Db\\Adapter\\Adapter'); $table = new TableGateway($mytable, $db); $results = $table->insert($data); $id = $table->lastInsertValue; return $id; }
/** * Create easypay trade. * @see \Zend\Mvc\Controller\AbstractActionController::indexAction() */ public function indexAction() { if ($this->verifyRequest()) { $this->appendTitle('收银台'); $this->layout()->setVariable('is_cashier_page', true); $dbAdapter = $this->getServiceLocator()->get('Zend\\Db\\Adapter\\Adapter'); /** * Check the parameters if they are valid. */ $parameters = $this->getRequest()->getQuery(); // if merchant exsist // if trade|redirect_url|notify_url|sign empty // if price is a floating numberic $exception = new \Exception('Parameters is invalid!'); if (empty(trim($parameters['trade'])) || empty(trim($parameters['redirect_url'])) || empty(trim($parameters['notify_url'])) || empty(trim($parameters['sign']))) { throw $exception; } if (!is_numeric(trim($parameters['price']))) { throw $exception; } $merchant_validator = new \Zend\Validator\Db\NoRecordExists(array('table' => 'merchant', 'field' => 'name', 'adapter' => $dbAdapter)); if ($merchant_validator->isValid(intval($parameters['merchant']))) { throw $exception; } /** * Check if there is an exsist trade. */ $GetMerchantIdByName = $this->getServiceLocator()->get('GetMerchantIdByName'); $merchant_id = $GetMerchantIdByName($parameters['merchant']); $trade = $this->getTrade($merchant_id, trim($parameters['trade'])); $trade_id = null; $payment_interface_type = null; if (!empty($trade)) { // Trade exsist. if ($trade->pay_status) { throw new \Exception('Trade had payed before!'); } else { // Update the exsist trade $trade->price = trim($parameters['price']); $trade->redirect_url = trim($parameters['redirect_url']); $trade->notify_url = trim($parameters['notify_url']); $trade->save(); $trade_id = $trade->id; $payment_interface_type = $trade->payment_interface_type; } } else { // Trade not exsist. // Create a new trade $tableGateway = new TableGateway('trade', $dbAdapter, new Feature\RowGatewayFeature('id')); $tableGateway->insert(array('merchant_id' => $merchant_id, 'merchant_trade_id' => trim($parameters['trade']), 'redirect_url' => trim($parameters['redirect_url']), 'notify_url' => trim($parameters['notify_url']), 'price' => trim($parameters['price']), 'pay_status' => 0, 'create_time' => date('Y-n-j H:i:s', time()))); $trade_id = $tableGateway->getLastInsertValue(); } return array('price' => trim($parameters['price']), 'merchant' => $parameters['merchant'], 'trade' => trim($parameters['trade']), 'selected_payment' => $payment_interface_type); } else { throw new \Exception('Bad Reaquest !'); } }
/** * Saves the given rating to the storage system. * * @param Rating $rating The rating to persist. */ public function persist(Rating $rating) { if ($rating->getId()) { $this->gateway->update(array('typeId' => $rating->getTypeId(), 'userId' => $rating->getUserId(), 'rating' => $rating->getRating()), array('id' => $rating->getId())); } else { $this->gateway->insert(array('typeId' => $rating->getTypeId(), 'userId' => $rating->getUserId(), 'rating' => $rating->getRating())); $id = $this->gateway->getLastInsertValue(); $rating->setId($id); } }
/** * @param array $data. * @return integer|null */ public function insert($data) { if (!$this->acl) { $this->acl = $this->aclLazyLoader->get(); } if (parent::insert($data)) { return (int) $this->getLastInsertValue(); } return null; }
/** * Adds a data */ public function insert() { $id = $this->table->insert($this->data); if (is_null($id)) { if (array_key_exists('firephp', $GLOBALS)) { $GLOBALS['firephp']->error('Error on query ' . $this->table->getSql()->getSqlPlatform()->getSqlString($this->table->getAdapter()->getPlatform())); } throw new Exception('Error on query ' . $this->table->getSql()->getSqlPlatform()->getSqlString($this->table->getAdapter()->getPlatform())); } $this->data[$this->primary] = $this->table->getLastInsertValue(); }
/** * Write session data * * @param string $id * @param string $data * @return bool */ public function write($id, $data) { $data = array($this->options->getModifiedColumn() => time(), $this->options->getDataColumn() => (string) $data); $rows = $this->tableGateway->select(array($this->options->getIdColumn() => $id, $this->options->getNameColumn() => $this->sessionName)); if ($row = $rows->current()) { return (bool) $this->tableGateway->update($data, array($this->options->getIdColumn() => $id, $this->options->getNameColumn() => $this->sessionName)); } $data[$this->options->getLifetimeColumn()] = $this->lifetime; $data[$this->options->getIdColumn()] = $id; $data[$this->options->getNameColumn()] = $this->sessionName; return (bool) $this->tableGateway->insert($data); }