Example #1
0
 /**
  *
  * Delete the data into the database according the table element
  * A primaryKey should be set. If the key is not set It can be obtained
  * from jqGridDB::getPrimaryKey
  * Return true on success, false when the operation is not succefull
  * @todo possibility to set additional where clause
  * @param array $data associative array which key values correspond to the
  * names in the delete command
  * @return boolean
  */
 public function delete(array $data, $where = '', array $params = null)
 {
     $result = false;
     if (!$this->del) {
         return $result;
     }
     //SQL Server hack
     if (!$this->checkPrimary()) {
         return $result;
     }
     $ide = null;
     $binds = array(&$ide);
     $types = array();
     if (count($data) > 0) {
         if ($where && strlen($where) > 0) {
             $id = "";
             $sql = "DELETE FROM " . $this->table . " WHERE " . $where;
             $stmt = $this->parseSql($sql, $params);
             $delids = "";
             $custom = true;
         } else {
             $id = $this->getPrimaryKeyId();
             if (!isset($data[$id])) {
                 $this->errorMessage = "Missed data id value to perform delete!";
                 if ($this->showError) {
                     $this->sendErrorHeader();
                 }
                 return $result;
             }
             $sql = "DELETE FROM " . $this->table . " WHERE " . $id . "=?";
             $stmt = $this->parseSql($sql, $binds, false);
             $delids = explode(",", $data[$id]);
             $custom = false;
         }
         $types[0] = 'custom';
         if ($stmt) {
             if ($this->trans) {
                 try {
                     jqGridDB::beginTransaction($this->pdo);
                     $result = $this->_actionsCRUDGrid('del', 'before');
                     if ($custom) {
                         if ($this->debug) {
                             $this->logQuery($sql, $params, false, $data, null, $this->primaryKey);
                         }
                         $result = jqGridDB::execute($stmt, $params);
                     } else {
                         foreach ($delids as $i => $ide) {
                             $delids[$i] = trim($delids[$i]);
                             $binds[0] =& $delids[$i];
                             if ($this->debug) {
                                 $this->logQuery($sql, $binds, $types, $data, $this->fields, $this->primaryKey);
                             }
                             jqGridDB::bindValues($stmt, $binds, $types);
                             $result = jqGridDB::execute($stmt, $binds);
                             if (!$result) {
                                 break;
                             }
                             unset($binds[0]);
                         }
                     }
                     jqGridDB::closeCursor($stmt);
                     if ($result) {
                         $result = $this->_actionsCRUDGrid('del', 'after');
                     }
                     if ($result) {
                         jqGridDB::commit($this->pdo);
                     } else {
                         $this->errorMessage = jqGridDB::errorMessage($this->pdo);
                         throw new Exception($this->errorMessage);
                     }
                 } catch (Exception $e) {
                     jqGridDB::rollBack($this->pdo);
                     $result = false;
                     if (!$this->errorMessage) {
                         $this->errorMessage = $e->getMessage();
                     }
                 }
             } else {
                 try {
                     $result = $this->_actionsCRUDGrid('del', 'before');
                     if ($result) {
                         if ($custom) {
                             $result = jqGridDB::execute($stmt, $params);
                         } else {
                             foreach ($delids as $i => $ide) {
                                 $delids[$i] = trim($delids[$i]);
                                 $binds[0] =& $delids[$i];
                                 if ($this->debug) {
                                     $this->logQuery($sql, $binds, $types, $data, $this->fields, $this->primaryKey);
                                 }
                                 jqGridDB::bindValues($stmt, $binds, $types);
                                 $result = jqGridDB::execute($stmt, $binds);
                                 if (!$result) {
                                     break;
                                 }
                                 unset($binds[0]);
                             }
                         }
                     }
                     jqGridDB::closeCursor($stmt);
                     if ($result) {
                         $result = $this->_actionsCRUDGrid('del', 'after');
                     }
                     if (!$result) {
                         $this->errorMessage = jqGridDB::errorMessage($this->pdo);
                         throw new Exception($this->errorMessage);
                     }
                 } catch (Exception $e) {
                     $result = false;
                     if (!$this->errorMessage) {
                         $this->errorMessage = $e->getMessage();
                     }
                 }
             }
         }
     }
     if ($this->debug) {
         $this->debugout();
     }
     if ($this->showError && !$result) {
         $this->sendErrorHeader();
     }
     return $result;
 }
Example #2
0
 public function delete(array $data)
 {
     $result = false;
     if (!$this->del) {
         return $result;
     }
     $ide = null;
     $binds = array(&$ide);
     $types = array();
     if (count($data) > 0) {
         $id = $this->getPrimaryKeyId();
         if (!isset($data[$id])) {
             return $result;
         }
         $sql = "DELETE FROM " . $this->table . " WHERE " . $id . "=?";
         $stmt = $this->parseSql($sql, $binds, false);
         $delids = explode(",", $data[$id]);
         $types[0] = 'custom';
         if ($stmt) {
             if ($this->trans) {
                 jqGridDB::beginTransaction($this->pdo);
                 foreach ($delids as $i => $ide) {
                     $delids[$i] = trim($delids[$i]);
                     $binds[0] =& $delids[$i];
                     if ($this->debug) {
                         $this->logQuery($sql, $binds, $types, $data, $this->fields, $this->primaryKey);
                     }
                     jqGridDB::bindValues($stmt, $binds, $types);
                     $test = jqGridDB::execute($stmt, $binds);
                     if (!$test) {
                         jqGridDB::rollBack($this->pdo);
                         break;
                         $result = false;
                     }
                     unset($binds[0]);
                 }
                 if ($test) {
                     $result = jqGridDB::commit($this->pdo);
                 }
             } else {
                 foreach ($delids as $i => $ide) {
                     $delids[$i] = trim($delids[$i]);
                     $binds[0] =& $delids[$i];
                     if ($this->debug) {
                         $this->logQuery($sql, $binds, $types, $data, $this->fields, $this->primaryKey);
                     }
                     jqGridDB::bindValues($stmt, $binds, $types);
                     $test = jqGridDB::execute($stmt, $binds);
                     if (!$test) {
                         break;
                         $result = false;
                     }
                     unset($binds[0]);
                 }
                 $result = true;
             }
             jqGridDB::closeCursor($stmt);
         }
     }
     if ($this->debug) {
         $this->debugout();
     }
     return $result;
 }