Skip to content

Commit b81b1a9

Browse files
committed
Add Comment::save() function
1 parent 28fd0f6 commit b81b1a9

File tree

1 file changed

+63
-3
lines changed

1 file changed

+63
-3
lines changed

src/libraries/Comment.php

Lines changed: 63 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919

2020
class Comment implements JsonSerializable {
2121

22+
const CACHE_TTL = 300;
23+
2224
const PARENT_TYPE_DOCUMENT = 0;
2325
const PARENT_TYPE_COMMENT = 1;
2426
const PARENT_TYPE_NEWS_POST = 2;
@@ -160,11 +162,11 @@ public static function getAll($parent_type, $parent_id) {
160162
$ids[] = (int) $row->id;
161163
$objects[] = new self($row);
162164
Common::$cache->set(
163-
"bnetdocs-comment-" . $row->id, serialize($row), 300
165+
"bnetdocs-comment-" . $row->id, serialize($row), self::CACHE_TTL
164166
);
165167
}
166168
$stmt->closeCursor();
167-
Common::$cache->set($ck, implode(",", $ids), 300);
169+
Common::$cache->set($ck, implode(",", $ids), self::CACHE_TTL);
168170
return $objects;
169171
} catch (PDOException $e) {
170172
throw new QueryException("Cannot refresh comment", $e);
@@ -314,12 +316,70 @@ public function refresh() {
314316
$this->parent_id = $row->parent_id;
315317
$this->parent_type = $row->parent_type;
316318
$this->user_id = $row->user_id;
317-
Common::$cache->set($ck, serialize($row), 300);
319+
Common::$cache->set($ck, serialize($row), self::CACHE_TTL);
318320
return true;
319321
} catch (PDOException $e) {
320322
throw new QueryException("Cannot refresh comment", $e);
321323
}
322324
return false;
323325
}
324326

327+
public function save() {
328+
if (!isset(Common::$database)) {
329+
Common::$database = DatabaseDriver::getDatabaseObject();
330+
}
331+
try {
332+
$stmt = Common::$database->prepare('
333+
UPDATE
334+
`comments`
335+
SET
336+
`content` = :content,
337+
`created_datetime` = :created_dt,
338+
`edited_count` = :edited_count,
339+
`edited_datetime` = :edited_dt,
340+
`parent_id` = :parent_id,
341+
`parent_type` = :parent_type,
342+
`user_id` = :user_id
343+
WHERE
344+
`id` = :id
345+
LIMIT 1;
346+
');
347+
$stmt->bindParam(':content', $this->content, PDO::PARAM_STR);
348+
$stmt->bindParam(':created_dt', $this->created_datetime, PDO::PARAM_INT);
349+
$stmt->bindParam(':edited_count', $this->edited_count, PDO::PARAM_INT);
350+
$stmt->bindParam(':edited_dt', $this->edited_datetime, PDO::PARAM_INT);
351+
$stmt->bindParam(':id', $this->id, PDO::PARAM_INT);
352+
$stmt->bindParam(':parent_id', $this->parent_id, PDO::PARAM_INT);
353+
$stmt->bindParam(':parent_type', $this->parent_type, PDO::PARAM_INT);
354+
$stmt->bindParam(':user_id', $this->user_id, PDO::PARAM_INT);
355+
if (!$stmt->execute()) {
356+
throw new QueryException( 'Cannot save comment' );
357+
}
358+
$stmt->closeCursor();
359+
360+
$object = new StdClass();
361+
$object->content = $this->content;
362+
$object->created_datetime = $this->created_datetime;
363+
$object->edited_count = $this->edited_count;
364+
$object->edited_datetime = $this->edited_datetime;
365+
$object->id = $this->id;
366+
$object->parent_id = $this->parent_id;
367+
$object->parent_type = $this->parent_type;
368+
$object->user_id = $this->user_id;
369+
370+
Common::$cache->set(
371+
'bnetdocs-comment-' . $this->id, serialize( $object ), self::CACHE_TTL
372+
);
373+
374+
Common::$cache->delete(
375+
'bnetdocs-comment-' . $this->parent_type . '-' . $this->parent_id
376+
);
377+
378+
return true;
379+
} catch (PDOException $e) {
380+
throw new QueryException( 'Cannot save comment', $e );
381+
}
382+
return false;
383+
}
384+
325385
}

0 commit comments

Comments
 (0)