Пример #1
0
 public function save(\App\Mail\Alert $alert)
 {
     $alerts = $this->fetchAll();
     $fopen = fopen($this->_filename, "a");
     flock($fopen, LOCK_EX);
     $fpNewFile = fopen($this->_filename . ".new", "w");
     flock($fpNewFile, LOCK_EX);
     fputcsv($fpNewFile, $this->_header, ",", '"');
     $updated = false;
     foreach ($alerts as $a) {
         if ($a->id == $alert->id) {
             $a = $alert;
             $updated = true;
         }
         fputcsv($fpNewFile, $a->toArray(), ",", '"');
     }
     if (!$updated && !$alert->id) {
         $alert->id = sha1(uniqid());
         fputcsv($fpNewFile, $alert->toArray(), ",", '"');
     }
     fclose($fpNewFile);
     fclose($fopen);
     file_put_contents($this->_filename, file_get_contents($this->_filename . ".new"));
     unlink($this->_filename . ".new");
     return $this;
 }
Пример #2
0
 public function save(\App\Mail\Alert $alert, $forceInsert = false)
 {
     $options = $alert->toArray();
     if (!$alert->id || $forceInsert) {
         $options["user_id"] = $this->_user->getId();
         if (!$alert->id) {
             $id = sha1(uniqid());
             $alert->id = $id;
         }
         $options["idstr"] = $alert->id;
         unset($options["id"]);
         $sqlOptions = array();
         foreach ($options as $name => $value) {
             if ($value === null) {
                 $value = "NULL";
             } elseif (is_bool($value)) {
                 $value = (int) $value;
             } elseif (!is_numeric($value)) {
                 $value = "'" . $this->_connection->real_escape_string($value) . "'";
             }
             $sqlOptions[$name] = $value;
         }
         $this->_connection->query("INSERT INTO " . $this->_table . " (`" . implode("`, `", array_keys($options)) . "`, `date_created`) VALUES (" . implode(", ", $sqlOptions) . ", NOW())");
     } else {
         $idStr = $options["id"];
         $sqlOptions = array();
         unset($options["id"]);
         foreach ($options as $name => $value) {
             if ($value === null) {
                 $value = "NULL";
             } elseif (is_bool($value)) {
                 $value = (int) $value;
             } elseif (!is_numeric($value)) {
                 $value = "'" . $this->_connection->real_escape_string($value) . "'";
             }
             $sqlOptions[] = "`" . $name . "` = " . $value;
         }
         $this->_connection->query("UPDATE " . $this->_table . " SET\n                " . implode(",", $sqlOptions) . " WHERE idstr = '" . $this->_connection->real_escape_string($idStr) . "'");
     }
     return $this;
 }