/** * @return array */ public static function loadAll() { $dbWrapper = new DatabaseWrapper(); $connection = $dbWrapper->getConnection(); $query = "SELECT * FROM temos;"; $temos = []; foreach ($connection->query($query) as $row) { $tema = new Tema($connection); $tema->setId($row['id']); $tema->setDate($row['subject_date']); $tema->setName($row['name']); $query = 'SELECT * FROM comments INNER JOIN temos ON comments.subjectId = ' . $row['id'] . " AND temos.id = " . $row['id'] . ";"; $comments = []; foreach ($connection->query($query) as $i) { $comment = new Comment($connection); $comment->setId($i['id']); $comment->setsubjectId($i['subjectId']); $comment->setText($i['text']); $comment->setDate($i['date']); $comment->setAuthor($i['author']); $comments[] = $comment; } foreach ($comments as $comment) { $tema->setComments($comment); } $temos[] = $tema; } return $temos; }
public function __construct($connection = null) { if (!isset($connection)) { $dbWrapper = new DatabaseWrapper(); $this->connection = $dbWrapper->getConnection(); } else { $this->connection = $connection; } }
<?php require_once 'Comment.php'; require_once '../databaseWrapper.php'; $dbWrapper = new DatabaseWrapper(); $connection = $dbWrapper->getConnection(); $ids = []; foreach ($connection->query('SELECT id FROM temos') as $row) { $ids[] = $row['id']; } $text = "Lambada lambada lambada"; $comment = new Comment($connection); $comment->setAuthor('Anonymous'); $comment->setSubjectID($ids[rand(0, sizeof($ids) - 1)]); $comment->setText($text); $comment->save(); header('Location: Controller.php');