/**
  * Stores a given notification in the provided db
  *
  * @param ClientNotification $notification A notification to be store in the specified db
  * @return int the id of notification inserted
  * @throws DatabaseException If an error occured in the storing of the notification in the db
  */
 public function store(ClientNotification $notification)
 {
     //String conversion and escaping in order to perform a correct and safe sql query
     $user_id = $notification->getUserId();
     $message = $this->dbConnection->getEscapedString((string) $notification->getMessage());
     $mediums = $notification->getNotificationMediums();
     try {
         //Notification insert
         $this->dbOperations->insert('user_id,message', $user_id . ",'" . $message . "'");
         //Id of the record just inserted
         $recordId = $this->dbOperations->getLastId();
         if (is_array($mediums)) {
             foreach ($mediums as $medium) {
                 //TODO this code doesn't work, use the code in the 'else' branch. Since the code will become the same it's a better choice to insert it into a method
                 $this->dbOperations->insert('notification_id,type_id', $recordId . ", '" . get_class($medium) . "'", $this->notificationTypeTable);
             }
         } else {
             $className = get_class($mediums);
             $queryResult = $this->dbOperations->getValue('id', 'name = ' . "'" . $className . "'", $this->typesTable);
             if (is_null($queryResult)) {
                 $this->dbOperations->insert('name', "'" . $className . "'", $this->typesTable);
                 $queryResult = $this->dbOperations->getLastId();
             }
             $this->dbOperations->insert('notification_id,type_id', $recordId . ", " . $queryResult, $this->notificationTypeTable);
         }
         return $recordId;
     } catch (MysqltcsException $e) {
         throw new DatabaseException('An error occurred in the insertion of the data in the database', 0, $e);
     }
 }
Exemplo n.º 2
0
}
print "<h3>Error on query</h3>\n";
//if an error is occurred mysqltcs throw an exception
try {
    $results = $connection->executeQuery("No sql");
} catch (MysqltcsException $e) {
    print "Error on query caught<br/>\n";
    print $e->getMessage() . "<br/>\n";
}
print "<h1>Simple operations</h1>\n";
//now we use MysqltcsOperations that allows to make common operations immediately in a specified table
$operations = new MysqltcsOperations($connection, $db['tables']['test1']);
print "<h2>Insert</h2>\n";
print "<h2>Wrong insert</h2>\n";
try {
    $operations->insert("value1gg", "'tt'");
} catch (MysqltcsException $e) {
    print "Error on insert caught<br/>\n";
    print $e->getMessage() . "<br/>\n";
}
print "<h2>Multiple insert(value - value2)</h2>\n";
$operations->insert("value,value2", array("'1','aa'", "'2','ba'"));
print "<h2>Simple insert(value - value2)</h2>\n";
$operations->insert("value,value2", "'1','bb'");
print "<h2>Id of last insert</h2>\n";
print $id = $operations->getLastId();
print "<h2>Get</h2>\n";
print "obviously you can catch exception if you make a wrong get";
print "<h3>Get 'value2' of {$id}</h3>\n";
print $operations->getValue("value2", "id = {$id}");
print "<h3>Get all data</h3>\n";