public function saveItem($arrParam = null, $options = null)
 {
     if ($options['task'] == 'add') {
         $sqlObj = new \Zend\Db\Sql\Sql($this->adapter);
         $insertObj = $sqlObj->insert('service_account_register');
         $insertObj->values($arrParam);
         $sqlString = $sqlObj->getSqlStringForSqlObject($insertObj);
         $this->adapter->query($sqlString)->execute();
     }
     if ($options['task'] == 'edit') {
         $this->tableGateway->update($arrParam, array('id' => $arrParam['id']));
     }
     if ($options['task'] == 'multi-status') {
         if (!empty($arrParam)) {
             foreach ($arrParam['id'] as $key => $value) {
                 if ($arrParam['type'] == 'multi-active') {
                     $status = 1;
                 }
                 if ($arrParam['type'] == 'multi-in-active') {
                     $status = 0;
                 }
                 $data = array('id' => $value, 'status' => $status);
                 $this->tableGateway->update($data, array('id' => $value));
             }
         }
     }
 }
 public function insert($data)
 {
     $insert = $this->sql->insert($this->_table);
     $insert->values($data);
     $result = $this->_execute($insert);
     return $result->getGeneratedValue();
 }
Exemple #3
0
 /**
  * Adds a new record of the Room object in the database.
  * @param Room $room - a populated object to add.
  */
 public function addNew(Room $room)
 {
     $adapter = $this->getServiceLocator()->get('Zend\\Db\\Adapter\\Adapter');
     $sql = new Sql($adapter);
     $insert = $sql->insert('room')->columns(array('number', 'name', 'building', 'attendant', 'posts'))->values(array('number' => $room->number, 'name' => $room->name, 'building' => $room->building, 'attendant' => $room->attendant, 'posts' => $room->posts));
     $sql->prepareStatementForSqlObject($insert)->execute();
 }
 /**
  * Saves a user to a database
  *
  * @param array $user
  */
 public function saveUser(array $user)
 {
     $sql = new Sql($this->dbAdapter);
     $insert = $sql->insert('users');
     $insert->columns(array_keys($user))->values($user);
     $sql->prepareStatementForSqlObject($insert)->execute();
 }
 /**
  * @return bool $successfulCreate
  */
 protected function create() : bool
 {
     $insert = $this->sql->insert($this->getTablename());
     $id = $this->adapter->getDriver()->getConnection()->getLastGeneratedValue();
     $this->load($id);
     $success = true;
     return $success;
 }
Exemple #6
0
 public function addRole($serviceManager, $userRoleTable, $userIdField, $roleIdField, $userId, $roleId)
 {
     $adapter = $serviceManager->get('Zend\\Db\\Adapter\\Adapter');
     $sql = new Sql($adapter);
     $insert = $sql->insert()->into($userRoleTable)->columns(array($userIdField, $roleIdField))->values(array($userId, $roleId));
     $sqlString = $sql->getSqlStringForSqlObject($insert);
     $execute = $adapter->query($sqlString, $adapter::QUERY_MODE_EXECUTE);
     return $execute;
 }
Exemple #7
0
 public function insert($data)
 {
     $adapter = $this->adapter;
     $sql = new Sql($this->adapter);
     $insert = $sql->insert('item_price');
     $insert->values($data);
     $selectString = $sql->getSqlStringForSqlObject($insert);
     return $results = $this->adapter->query($selectString, $adapter::QUERY_MODE_EXECUTE);
 }
Exemple #8
0
 /**
  * 
  * @param string $tableName
  * @param array $data
  * @return bool
  */
 public function insert($tableName, array $data)
 {
     $sql = new Sql($this->adapter);
     $insert = $sql->insert($tableName);
     $insert->values($data);
     $sqlString = $sql->getSqlStringForSqlObject($insert);
     $results = $this->adapter->query($sqlString, Adapter::QUERY_MODE_EXECUTE);
     return $results;
 }
 /**
  * Insert
  *
  * @param  array $set
  * @return int
  */
 public function insert($set)
 {
     if (!$this->isInitialized) {
         $this->initialize();
     }
     $insert = $this->sql->insert();
     $insert->values($set);
     return $this->executeInsert($insert);
 }
Exemple #10
0
 /**
  * Insert
  * 
  * @param  array $set
  * @return int
  */
 public function insert($set)
 {
     $insert = $this->sql->insert();
     $insert->values($set);
     $statement = $this->sql->prepareStatementForSqlObject($insert);
     $result = $statement->execute();
     $this->lastInsertValue = $this->adapter->getDriver()->getConnection()->getLastGeneratedValue();
     return $result->getAffectedRows();
 }
Exemple #11
0
 public function createTransaction($data)
 {
     $adapter = $this->adapter;
     $sql = new Sql($this->adapter);
     $insert = $sql->insert('user_transaction');
     $insert->values($data);
     $selectString = $sql->getSqlStringForSqlObject($insert);
     $results = $this->adapter->query($selectString, $adapter::QUERY_MODE_EXECUTE);
     return array("trnx_id" => $results->getGeneratedValue());
 }
 public function create(array $data)
 {
     $sql = new Sql($this->adapter);
     $this->adapter->getDriver()->getConnection()->beginTransaction();
     $insert = $sql->insert('places');
     $insert->values(['name' => $data['name'], 'latitude' => $data['latitude'], 'longitude' => $data['longitude']]);
     $query = $sql->getSqlStringForSqlObject($insert);
     /** @var Result $results */
     $results = $this->adapter->query($query, Adapter::QUERY_MODE_EXECUTE);
     $placeId = $results->getGeneratedValue();
     foreach ($data['types'] as $typeId) {
         $insert = $sql->insert('places_place_types');
         $insert->values(['place_id' => $placeId, 'place_type_id' => $typeId]);
         $q = $sql->getSqlStringForSqlObject($insert);
         $this->adapter->query($q, Adapter::QUERY_MODE_EXECUTE);
     }
     $this->adapter->getDriver()->getConnection()->commit();
     return $placeId;
 }
 /**
  * Adds a new record of the Equipment object in the database.
  * @param Equipment $equipment - a populated object to add.
  */
 public function addNew(Equipment $equipment)
 {
     if (!$this->is_init()) {
         throw new \Exception("Equipments collection uninitiated.");
     }
     $adapter = $this->getServiceLocator()->get('Zend\\Db\\Adapter\\Adapter');
     $sql = new Sql($adapter);
     $insert = $sql->insert('equipment')->columns(array('cid', 'name', 'quantity', 'destiny', 'damaged', 'hire', 'adddate'))->values(array('cid' => $equipment->cid, 'name' => $equipment->name, 'quantity' => $equipment->quantity, 'destiny' => $equipment->destiny, 'damaged' => $equipment->damaged, 'hire' => $equipment->hire, 'adddate' => $equipment->adddate));
     $sql->prepareStatementForSqlObject($insert)->execute();
 }
 public function create($id)
 {
     $sql = new Sql($this->getAdapter());
     $insert = $sql->insert($this->getTableName());
     $insert->values(['id' => (string) $id]);
     $query = $sql->getSqlStringForSqlObject($insert);
     /** @var Result $results */
     $results = $this->adapter->query($query, Adapter::QUERY_MODE_EXECUTE);
     return $results->getGeneratedValue();
 }
 public function saveHuntClubsHasPlayers($data)
 {
     $adapter = $this->tableGateway->getAdapter();
     $sql = new Sql($adapter);
     $insert = $sql->insert('hunt_clubs_has_players');
     $insert->values($data);
     $statement = $sql->prepareStatementForSqlObject($insert);
     $results = $statement->execute();
     $id = $adapter->getDriver()->getLastGeneratedValue();
     return $id;
 }
 public function saveConsumables($data)
 {
     $dbAdapter = $this->getServiceLocator()->get('Zend\\Db\\Adapter\\Adapter');
     $sql = new Sql($dbAdapter);
     $insert = $sql->insert('weapon_consumables_has_players');
     $insert->values($data);
     $statement = $sql->prepareStatementForSqlObject($insert);
     $results = $statement->execute();
     $id = $dbAdapter->getDriver()->getLastGeneratedValue();
     //print 'I am here !' . $id; exit();
     return $id;
 }
 public function insert($data)
 {
     $adapter = $this->adapter;
     $sql = new Sql($this->adapter);
     $insert = $sql->insert('order_products');
     $insert->values($data);
     $selectString = $sql->getSqlStringForSqlObject($insert);
     $results = $this->adapter->query($selectString, $adapter::QUERY_MODE_EXECUTE);
     //if type package is there then reduce number of clothes and days
     if ($results && $data['order_type'] == 'package') {
         $this->updatePackage($data['order_id'], $data['quantity']);
     }
 }
Exemple #18
0
 /**
  * Save
  *
  * @return integer
  */
 public function save()
 {
     $this->initialize();
     if ($this->rowExistsInDatabase()) {
         // UPDATE
         $data = $this->data;
         $where = array();
         // primary key is always an array even if its a single column
         foreach ($this->primaryKeyColumn as $pkColumn) {
             $where[$pkColumn] = $this->primaryKeyData[$pkColumn];
             if ($data[$pkColumn] == $this->primaryKeyData[$pkColumn]) {
                 unset($data[$pkColumn]);
             }
         }
         $statement = $this->sql->prepareStatementForSqlObject($this->sql->update()->set($data)->where($where));
         $result = $statement->execute();
         $rowsAffected = $result->getAffectedRows();
         unset($statement, $result);
         // cleanup
     } else {
         // INSERT
         $insert = $this->sql->insert();
         $insert->values($this->data);
         $statement = $this->sql->prepareStatementForSqlObject($insert);
         $result = $statement->execute();
         if (($primaryKeyValue = $result->getGeneratedValue()) && count($this->primaryKeyColumn) == 1) {
             $this->primaryKeyData = array($this->primaryKeyColumn[0] => $primaryKeyValue);
         } else {
             // make primary key data available so that $where can be complete
             $this->processPrimaryKeyData();
         }
         $rowsAffected = $result->getAffectedRows();
         unset($statement, $result);
         // cleanup
         $where = array();
         // primary key is always an array even if its a single column
         foreach ($this->primaryKeyColumn as $pkColumn) {
             $where[$pkColumn] = $this->primaryKeyData[$pkColumn];
         }
     }
     // refresh data
     $statement = $this->sql->prepareStatementForSqlObject($this->sql->select()->where($where));
     $result = $statement->execute();
     $rowData = $result->current();
     unset($statement, $result);
     // cleanup
     // make sure data and original data are in sync after save
     $this->populate($rowData, true);
     // return rows affected
     return $rowsAffected;
 }
 public function save()
 {
     $sql = new Sql($this->adapter, TestRecord::TABLE);
     $sqlObject = null;
     if ($this->id) {
         // update existing
         $sqlObject = $sql->update()->set(["title" => $this->title, "created_on" => $this->created_on, "type" => $this->type])->where(["id" => $this->id]);
     } else {
         // insert new
         $sqlObject = $sql->insert()->columns(["title", "created_on", "type"])->values(["title" => $this->title, "created_on" => $this->created_on, "type" => $this->type]);
     }
     $statement = $sql->prepareStatementForSqlObject($sqlObject);
     $results = $statement->execute();
 }
 /**
  * Writes the database back to the database
  */
 protected function save()
 {
     $this->validate();
     $zend_db = Database::getConnection();
     $sql = new Sql($zend_db, $this->tablename);
     if ($this->getId()) {
         $update = $sql->update()->set($this->data)->where(array('id' => $this->getId()));
         $sql->prepareStatementForSqlObject($update)->execute();
     } else {
         $insert = $sql->insert()->values($this->data);
         $sql->prepareStatementForSqlObject($insert)->execute();
         $this->data['id'] = $zend_db->getDriver()->getLastGeneratedValue();
     }
 }
Exemple #21
0
    /**
     * Save
     *
     * @return integer
     */
    public function save()
    {
        if (is_array($this->primaryKeyColumn)) {
            // @todo compound primary keys
            throw new Exception\RuntimeException('Compound primary keys are currently not supported, but are on the TODO list.');
        }

        if (isset($this->originalData[$this->primaryKeyColumn])) {

            // UPDATE
            $where = array($this->primaryKeyColumn => $this->originalData[$this->primaryKeyColumn]);
            $data = $this->data;
            unset($data[$this->primaryKeyColumn]);

            $statement = $this->sql->prepareStatementForSqlObject($this->sql->update()->set($data)->where($where));
            $result = $statement->execute();
            $rowsAffected = $result->getAffectedRows();
            unset($statement, $result); // cleanup

        } else {

            // INSERT
            $insert = $this->sql->insert();
            $insert->values($this->data);

            $statement = $this->sql->prepareStatementForSqlObject($insert);

            $result = $statement->execute();
            $primaryKeyValue = $result->getGeneratedValue();
            $rowsAffected = $result->getAffectedRows();
            unset($statement, $result); // cleanup

            $where = array($this->primaryKeyColumn => $primaryKeyValue);
        }

        // refresh data
        $statement = $this->sql->prepareStatementForSqlObject($this->sql->select()->where($where));
        $result = $statement->execute();
        $rowData = $result->current();
        unset($statement, $result); // cleanup

        $this->populateOriginalData($rowData);

        // return rows affected
        return $rowsAffected;
    }
Exemple #22
0
 /**
  * Grant right
  *
  * @param   \Zend\Db\Sql\Sql    $sql
  * @param   array               $data
  * @param   bool                $grant
  * @return  int
  */
 private function grant(Sql\Sql $sql, array $data, $grant = true)
 {
     if ($grant) {
         $select = $sql->select()->where($data);
         $result = $sql->prepareStatementForSqlObject($select)->execute();
         if ($result->getAffectedRows()) {
             return 0;
         } else {
             $insert = $sql->insert()->values($data);
             $result = $sql->prepareStatementForSqlObject($insert)->execute();
             return $result->getAffectedRows();
         }
     } else {
         $delete = $sql->delete()->where($data);
         $result = $sql->prepareStatementForSqlObject($delete)->execute();
         return $result->getAffectedRows();
     }
 }
Exemple #23
0
 public function _insert(Servicos $servicos)
 {
     $adapter = $this->ConfAdapter();
     $servicos = (array) $servicos;
     $sql = new Sql($adapter);
     if ($servicos['id'] == 0) {
         $insert = $sql->insert('servicos')->values($servicos);
     } else {
         $insert = $sql->update('servicos')->set($servicos)->where(array('id' => $servicos['id']));
     }
     $sqlString = $sql->getSqlStringForSqlObject($insert);
     $adapter->query($sqlString, Adapter::QUERY_MODE_EXECUTE);
     $statement = $adapter->query('select id from servicos order by id desc limit 1');
     $stm = $statement->execute();
     foreach ($stm as $dados) {
         $result = $dados['id'];
     }
     return $result;
 }
Exemple #24
0
 public function refresh($user_id)
 {
     $adapter = $this->sm->get('Zend\\Db\\Adapter\\Adapter');
     $sql = new Sql($adapter);
     do {
         //Generate new hash
         $hash = Application\Model\Util::unique_id();
         //Check if hash is taken
         $select = $sql->select('tokens_table')->where(array('hash' => $hash));
         $selectString = $sql->getSqlStringForSqlObject($select);
         $results = $adapter->query($selectString, $adapter::QUERY_MODE_EXECUTE)->toArray()[0];
     } while ($results);
     //Disable old hashs
     $update = $sql->update('tokens_table')->set(array('enabled' => 0))->where(array('user_id' => $user_id));
     $selectString = $sql->getSqlStringForSqlObject($update);
     $adapter->query($selectString, $adapter::QUERY_MODE_EXECUTE);
     //Create new hash on table
     $insert = $sql->insert('tokens_table')->values(array('user_id' => $user_id, 'hash' => $hash, 'enabled' => true));
     $selectString = $sql->getSqlStringForSqlObject($insert);
     $adapter->query($selectString, $adapter::QUERY_MODE_EXECUTE);
 }
Exemple #25
0
 /**
  * Store instance data in the database
  *
  * @throws \Thinkscape\ActiveRecord\Exception\DatabaseException
  * @throws \Thinkscape\ActiveRecord\Exception\RuntimeException
  * @return void
  */
 public function save()
 {
     $db = $this->getDb();
     $sql = new Sql($db);
     if (!$this->id) {
         // perform an INSERT operation
         // Get the data
         $updateData = $this->collectUpdateData();
         // Prepare and execute INSERT
         $insert = $sql->insert($this->getDbTable())->values($updateData);
         $db->query($insert->getSqlString($db->getPlatform()), $db::QUERY_MODE_EXECUTE);
         // Remember ID
         if (!($this->id = $db->getDriver()->getLastGeneratedValue())) {
             throw new Exception\DatabaseException(sprintf('Unable to retrieve INSERT id when trying to persist %s', get_class($this)));
         }
         // Store in registry
         Core::storeInstanceInRegistry(get_called_class(), $this->id, $this);
     } else {
         // perform an UPDATE operation
         if (!$this->isLoaded) {
             throw new Exception\RuntimeException('Attempt to save() a record that has not been loaded');
         }
         // Get the data
         $updateData = $this->collectUpdateData();
         if (!count($updateData)) {
             return;
             // there is nothing to update
         }
         // Prepare and execute UPDATE
         $insert = $sql->update($this->getDbTable())->set($updateData);
         $result = $db->query($insert->getSqlString($db->getPlatform()))->execute();
         // Check if successful
         if ($result->getAffectedRows() == 0) {
             throw new Exception\DatabaseException(sprintf('Unable to update record #%s in table %s (class %s)', $this->id, $this->getDbTable(), get_class($this)));
         }
     }
 }
 public function save($sm, $recid, $record, $where, $original = array(), $lastid = array(), $remove = array(), $centais = array(), $modifier = array())
 {
     /*     $sm        - in Controller $this->getServiceLocator();
      * $recid     - record ID in table
      * $record    - array of values to save. key: <table_name>-<field_name> value: <value>
      * $where     - where to save. key: <table_name>-<field_name> value: <key in $record array>
      * $original  - array of original form values. To check for duplicates where duplicates not alowed.	Check performed if value in $original not equal to value in $record.
      * 				key: <table_name_field_name>-<field_name> value: <value>
      * $lastid    - "prime record on insert" array. key: <where to use table>-<where to use field> value: <source table>-<source field>
      * 				! in $record's array "source" tables fields must be on top
      * $remove    - array of fields to delete and insert on edit action. key: <table_name>-<field_name> value: source of field value in form <table_name>-<field_name>
      * $centais   - array of fields to multiply by fixed values. key: <field_name> value: <fixed value>
      * $modifier  - if it not empty, 'modifier' and 'modified' fields are added to array of records
      *
      */
     $adapter = $sm->get('Zend\\Db\\Adapter\\Adapter');
     $sql = new Sql($adapter);
     $config = $sm->get('Config');
     $tables = array();
     foreach ($record as $key => $value) {
         $parts = explode('-', $key);
         if (isset($parts[1])) {
             if (is_array($value) && !empty($value)) {
                 //-- if it 'enum' or 'list' field
                 if (isset($value[0]['id'])) {
                     //-- so it's 'enum'
                     foreach ($value as $ekey => $evalue) {
                         $tables[$parts[0]][$parts[1]][] = $evalue['id'];
                     }
                 } else {
                     //-- so it's 'list'
                     $tables[$parts[0]][$parts[1]] = $value['id'];
                 }
             } else {
                 if (isset($centais[$parts[1]])) {
                     //--- if in cents array
                     (int) ($value = $value * $centais[$parts[1]]);
                 }
                 $tables[$parts[0]][$parts[1]] = $value;
             }
         }
     }
     if (!is_array($recid)) {
         $r = array();
         foreach ($tables as $key => $value) {
             $r[$key] = $recid;
         }
         $recid = $r;
     }
     $removes = array();
     foreach ($remove as $key => $value) {
         $parts = explode('-', $key);
         $vparts = explode('-', $value);
         if (isset($parts[1]) && isset($vparts[1])) {
             $removes[$parts[0]][$parts[1]] = $tables[$vparts[0]][$vparts[1]];
         }
     }
     $lastids = array();
     foreach ($lastid as $key => $value) {
         $parts = explode('-', $key);
         $vparts = explode('-', $value);
         if (isset($parts[0]) && isset($vparts[1])) {
             $lastids[$vparts[0]][$vparts[1]][$parts[0]] = $parts[1];
         }
     }
     //-- to ensure that 'lastids' table in tables array goes first
     if (!empty($lastids)) {
         $tmp = array();
         foreach ($lastids as $key => $value) {
             if (array_key_exists($key, $tables)) {
                 $tmp[$key] = $tables[$key];
                 unset($tables[$key]);
             }
         }
         foreach ($tables as $key => $value) {
             $tmp[$key] = $value;
         }
         $tables = $tmp;
     }
     $wheres_orig = array();
     foreach ($original as $key => $value) {
         $parts = explode('-', $key);
         if (isset($parts[0]) && isset($parts[1])) {
             if (is_array($value)) {
                 //-- if that's a field of type 'list'
                 $value = $value['id'];
             }
             $wheres_orig[$parts[0]][$parts[1]] = $value;
         }
     }
     $wheres_new = array();
     foreach ($wheres_orig as $table => $array) {
         foreach ($array as $key => $value) {
             if (isset($tables[$table][$key])) {
                 $wheres_new[$table][$key] = $tables[$table][$key];
             }
         }
     }
     $wheres = array();
     foreach ($where as $key => $value) {
         $parts = explode('-', $key);
         $vparts = explode('-', $value);
         if (isset($parts[0]) && isset($parts[1]) && isset($tables[$vparts[0]][$vparts[1]])) {
             $wheres[$parts[0]][$parts[1]] = $tables[$vparts[0]][$vparts[1]];
             if (!empty($recid[$key])) {
                 unset($tables[$vparts[0]][$vparts[1]]);
             }
         }
     }
     $affectedRows = 0;
     $debug_mode = false;
     //		$debug_mode = true;  //-- uncoment if you just want to see queries in console; no writes to db performed
     if ($debug_mode) {
         echo "recid:\n";
         print_r($recid);
         echo "tables:\n";
         print_r($tables);
         echo "wheres:\n";
         print_r($wheres);
         echo "removes:\n";
         print_r($removes);
         echo "wheres_orig:\n";
         print_r($wheres_orig);
         echo "wheres_new:\n";
         print_r($wheres_new);
         echo "lastids:\n";
         print_r($lastids);
         //			exit;
     }
     $primeids = array();
     foreach ($tables as $table => $set) {
         if (!empty($this->table_filter)) {
             if (!isset($this->table_filter[$table])) {
                 continue;
             }
         }
         if (in_array($table, $config['ACL_TABLES'])) {
             $table_ident = new TableIdentifier($table, $config['ACL_DB']);
         } else {
             $table_ident = $table;
         }
         //-- non standart modifier and modified field names handling
         if (!empty($modifier)) {
             if (is_array($modifier)) {
                 if (isset($modifier[$table]['modifier_name'])) {
                     $set[$modifier[$table]['modifier_name']] = $modifier[$table]['modifier'];
                 }
                 if (isset($modifier[$table]['modified_name'])) {
                     $set[$modifier[$table]['modified_name']] = new Expression('NOW()');
                 }
             } else {
                 $modifier = $sm->get('Zend\\Authentication\\AuthenticationService')->getIdentity()->id;
                 $set['modifier'] = $modifier;
                 $set['modified'] = new Expression('NOW()');
             }
         }
         if (!$recid[$table] || isset($wheres_new[$table])) {
             //-- so it is insert or edit with possibly changed required unicue fields
             if (isset($wheres_orig[$table]) && $wheres_orig[$table] != $wheres_new[$table]) {
                 if ($this->duplicated($sm, $table_ident, $wheres_new[$table])) {
                     end($wheres_new[$table]);
                     $dataset = array('status' => 'success', 'validate' => array($table . '-' . key($wheres_new[$table]) => _('Record exists')));
                     return $dataset;
                 }
             }
             if ($recid[$table]) {
                 //-- if that is edit anyway
                 $dbobj = $sql->update($table_ident);
                 $dbobj->set($set);
                 foreach ($wheres[$table] as $where => $value) {
                     if (isset($wheres_orig[$table][$where])) {
                         $dbobj->where(array($where => $wheres_orig[$table][$where]));
                     } else {
                         $dbobj->where(array($where => $value));
                     }
                 }
             } else {
                 //-- if to insert
                 $dbobj = $sql->insert($table_ident);
                 if (isset($primeids[$table])) {
                     $current = current($set);
                     foreach ($primeids[$table] as $key => $value) {
                         if (empty($value) && !empty($tables[$table][$key])) {
                             $value = $tables[$table][$key];
                         }
                         if (is_array($current)) {
                             foreach ($current as $cvalue) {
                                 $set = array(key($set) => $cvalue, $key => $value);
                                 $dbobj->values($set);
                                 $query = $sql->getSqlStringForSqlObject($dbobj);
                                 if (!$debug_mode) {
                                     $result = $adapter->query($query, $adapter::QUERY_MODE_EXECUTE);
                                 } else {
                                     echo "*1* {$query}\n";
                                 }
                             }
                         } else {
                             if (!array_filter($set)) {
                                 continue;
                             }
                             $set[$key] = $value;
                             $dbobj->values($set);
                             $query = $sql->getSqlStringForSqlObject($dbobj);
                             if (!$debug_mode) {
                                 $result = $adapter->query($query, $adapter::QUERY_MODE_EXECUTE);
                             } else {
                                 echo "*2* {$query}\n";
                             }
                         }
                     }
                     continue;
                 }
                 $dbobj->values($set);
             }
             $query = $sql->getSqlStringForSqlObject($dbobj);
             if (!$debug_mode) {
                 $result = $adapter->query($query, $adapter::QUERY_MODE_EXECUTE);
                 $affectedRows += $result->count();
             } else {
                 echo "*3* {$query}\n";
             }
             if (isset($lastids[$table])) {
                 $lastId = $adapter->getDriver()->getLastGeneratedValue();
                 foreach ($lastids[$table] as $key => $value) {
                     foreach ($value as $key => $value) {
                         $primeids[$key][$value] = $lastId;
                     }
                 }
             }
             if (!$debug_mode) {
                 $affectedRows += $result->count();
             }
         } else {
             //-- edit
             if (isset($removes[$table])) {
                 //-- if have to delete existing records
                 $adapter->getDriver()->getConnection()->beginTransaction();
                 $insert_set = array();
                 $delete = $sql->delete($table_ident);
                 foreach ($removes[$table] as $where => $value) {
                     $insert_set[$where] = $value;
                     $delete->where("{$where} = '{$value}'");
                 }
                 $query = $sql->getSqlStringForSqlObject($delete);
                 if (!$debug_mode) {
                     $result = $adapter->query($query, $adapter::QUERY_MODE_EXECUTE);
                     $affectedRows += $result->count();
                 } else {
                     echo "*4* {$query}\n";
                 }
                 //-- so that's insert
                 foreach ($tables[$table] as $key => $set) {
                     if (empty($set)) {
                         continue;
                     }
                     if (!is_array($set)) {
                         //-- not an enum type field
                         $dbobj = $sql->insert($table_ident);
                         $dbobj->values(array_merge($tables[$table], $insert_set));
                         $query = $sql->getSqlStringForSqlObject($dbobj);
                         if (!$debug_mode) {
                             $result = $adapter->query($query, $adapter::QUERY_MODE_EXECUTE);
                         } else {
                             echo "*5* {$query}\n";
                         }
                         break;
                     } else {
                         foreach ($set as $ivalue) {
                             $dbobj = $sql->insert($table_ident);
                             $dbobj->values(array_merge(array($key => $ivalue), $insert_set));
                             $query = $sql->getSqlStringForSqlObject($dbobj);
                             if (!$debug_mode) {
                                 $result = $adapter->query($query, $adapter::QUERY_MODE_EXECUTE);
                             } else {
                                 echo "*6* {$query}\n";
                             }
                         }
                     }
                 }
                 $adapter->getDriver()->getConnection()->commit();
                 continue;
             }
             $update = $sql->update($table_ident);
             $update->set($set);
             foreach ($wheres[$table] as $where => $value) {
                 $update->where(array($where => $value));
             }
             $query = $sql->getSqlStringForSqlObject($update);
             if (!$debug_mode) {
                 $result = $adapter->query($query, $adapter::QUERY_MODE_EXECUTE);
                 $affectedRows += $result->count();
             } else {
                 echo "*7* {$query}\n";
             }
         }
     }
     $dataset = array('status' => 'success', 'message' => _('Affected rows: ') . $affectedRows);
     if (!empty($lastId)) {
         $dataset['last_id'] = $lastId;
     }
     return $dataset;
 }
 public function saveItem($arrParam = null, $options = null)
 {
     if ($options['task'] == 'user_send') {
         $data = array('user_id_receive' => $arrParam['user_id_nhan'], 'name' => $arrParam['name'], 'content' => $arrParam['content'], 'date_time' => $arrParam['date_time'], 'user_id_send' => $arrParam['user_id_send']);
         $sqlObj = new \Zend\Db\Sql\Sql($this->adapter);
         $insertObj = $sqlObj->insert('message_send');
         $insertObj->values($data);
         $sqlString = $sqlObj->getSqlStringForSqlObject($insertObj);
         $this->adapter->query($sqlString)->execute();
     }
     if ($options['task'] == 'user_receive') {
         $data = array('user_id_send' => $arrParam['user_id_send'], 'name' => $arrParam['name'], 'content' => $arrParam['content'], 'date_time' => $arrParam['date_time'], 'user_id_receive' => $arrParam['user_id_nhan']);
         $sqlObj = new \Zend\Db\Sql\Sql($this->adapter);
         $insertObj = $sqlObj->insert('message_receive');
         $insertObj->values($data);
         $sqlString = $sqlObj->getSqlStringForSqlObject($insertObj);
         $this->adapter->query($sqlString)->execute();
     }
 }
 /**
  * Insert entity data into the database
  *
  * @param  string $class Class name of object being created
  * @param  array  $data  The data to insert
  * @return int    The ID of newly inserted record
  */
 protected function injectDbWithEntityData($class, $data = [])
 {
     $table = $class::getStaticDbTable();
     $adapter = $this->adapter;
     $sql = new Sql($adapter);
     $select = $sql->insert($table)->values($data);
     $result = $adapter->query($select->getSqlString($adapter->getPlatform()))->execute();
     $this->assertNotNull($result->getAffectedRows());
     $id = $adapter->getDriver()->getLastGeneratedValue();
     $this->assertTrue(is_numeric($id));
     return (int) $id;
 }
 public function saveItem($arrParam = null, $options = null)
 {
     if ($options['task'] == 'add') {
         $this->tableGateway->insert($arrParam);
         return $this->tableGateway->lastInsertValue;
     }
     if ($options['task'] == 'edit') {
         $this->tableGateway->update($arrParam, array('id' => $arrParam['id']));
     }
     if ($options['task'] == 'contact-me') {
         $sqlObj = new Sql($this->adapter);
         $insertObj = $sqlObj->insert('contact_real_estate');
         $insertObj->values($arrParam);
         $sqlString = $sqlObj->getSqlStringForSqlObject($insertObj);
         $this->adapter->query($sqlString)->execute();
     }
     if ($options['task'] == 'save-real-estate-favorite') {
         $sqlObj = new Sql($this->adapter);
         $insertObj = $sqlObj->insert('favorite_real_estate');
         $insertObj->values($arrParam);
         $sqlString = $sqlObj->getSqlStringForSqlObject($insertObj);
         $this->adapter->query($sqlString)->execute();
     }
     if ($options['task'] == 'multi-status') {
         if (!empty($arrParam)) {
             foreach ($arrParam['id'] as $key => $value) {
                 if ($arrParam['type'] == 'multi-active') {
                     $status = 1;
                 }
                 if ($arrParam['type'] == 'multi-in-active') {
                     $status = 0;
                 }
                 $data = array('id' => $value, 'status' => $status);
                 $this->tableGateway->update($data, array('id' => $value));
             }
         }
     }
 }
Exemple #30
0
 /**
  * Method add migration to schema table
  *
  * @param string $module Module name
  * @param string $migration Migration name
  * @return $this
  */
 protected function pushMigration($module, $migration)
 {
     if (null === $module) {
         $module = '';
     }
     try {
         $sql = new Sql($this->db);
         $insert = $sql->insert($this->getMigrationsSchemaTable());
         $insert->values(array('module' => $module, 'migration' => $migration));
         $selectString = $sql->getSqlStringForSqlObject($insert);
         $this->db->query($selectString, Adapter::QUERY_MODE_EXECUTE);
     } catch (\Exception $e) {
         // table is not exist
     }
     return $this;
 }