public function find($id) { $response = []; try { if (!is_numeric($id)) { throw new \Exception("wrong id"); } $query = $this->db->prepare("SELECT * FROM komentarai WHERE id = :id LIMIT 1 "); $query->bindParam(':id', $id, \PDO::PARAM_INT); $query->execute(); $query->setFetchMode(\PDO::FETCH_OBJ); $result = $query->fetchObject(); $komentaras = new Komentaras(); if (!empty($result)) { $komentaras->setId($result->id); $komentaras->setData($result->data); $komentaras->setKomentaras($result->komentaras); $komentaras->setAutorius($result->autorius); $komentaras->setTemosId($result->temos_id); } $response['status'] = 'success'; $response['data'] = $komentaras; } catch (\Exception $e) { $response['status'] = 'failed'; $response['message'] = $e->getMessage(); } return $response; }
/** * @return array */ public function getComments(Tema $tema) { $response = []; try { if (!is_numeric($tema->getId())) { throw new \Exception('object is empty'); } $query = $this->db->prepare("\n SELECT *\n FROM komentarai\n WHERE temos_id = :temosid"); $query->bindParam(':temosid', $tema->getId(), \PDO::PARAM_INT); $query->execute(); $query->setFetchMode(\PDO::FETCH_OBJ); $comments = []; foreach (new \RecursiveArrayIterator($query->fetchAll()) as $r) { $komentaras = new Komentaras(); $komentaras->setId($r->id); $komentaras->setData($r->data); $komentaras->setKomentaras($r->komentaras); $komentaras->setAutorius($r->autorius); $komentaras->setTemosId($r->temos_id); $comments[] = $komentaras; } $response['status'] = 'success'; $response['data'] = $comments; } catch (\Exception $e) { $response['status'] = 'failed'; $response['message'] = $e->getMessage(); } return $response; }