|
19 | 19 |
|
20 | 20 | class Comment implements JsonSerializable { |
21 | 21 |
|
| 22 | + const CACHE_TTL = 300; |
| 23 | + |
22 | 24 | const PARENT_TYPE_DOCUMENT = 0; |
23 | 25 | const PARENT_TYPE_COMMENT = 1; |
24 | 26 | const PARENT_TYPE_NEWS_POST = 2; |
@@ -160,11 +162,11 @@ public static function getAll($parent_type, $parent_id) { |
160 | 162 | $ids[] = (int) $row->id; |
161 | 163 | $objects[] = new self($row); |
162 | 164 | Common::$cache->set( |
163 | | - "bnetdocs-comment-" . $row->id, serialize($row), 300 |
| 165 | + "bnetdocs-comment-" . $row->id, serialize($row), self::CACHE_TTL |
164 | 166 | ); |
165 | 167 | } |
166 | 168 | $stmt->closeCursor(); |
167 | | - Common::$cache->set($ck, implode(",", $ids), 300); |
| 169 | + Common::$cache->set($ck, implode(",", $ids), self::CACHE_TTL); |
168 | 170 | return $objects; |
169 | 171 | } catch (PDOException $e) { |
170 | 172 | throw new QueryException("Cannot refresh comment", $e); |
@@ -314,12 +316,70 @@ public function refresh() { |
314 | 316 | $this->parent_id = $row->parent_id; |
315 | 317 | $this->parent_type = $row->parent_type; |
316 | 318 | $this->user_id = $row->user_id; |
317 | | - Common::$cache->set($ck, serialize($row), 300); |
| 319 | + Common::$cache->set($ck, serialize($row), self::CACHE_TTL); |
318 | 320 | return true; |
319 | 321 | } catch (PDOException $e) { |
320 | 322 | throw new QueryException("Cannot refresh comment", $e); |
321 | 323 | } |
322 | 324 | return false; |
323 | 325 | } |
324 | 326 |
|
| 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 | + |
325 | 385 | } |
0 commit comments