public function getNewMessages() { $query_start = "SELECT a.*, \n COUNT(b.message_id) as likes, COUNT(distinct c.message_id) as didlike\n FROM messages a \n left outer join likes b\n on a.id = b.message_id\n left outer join likes c\n on a.id = c.message_id AND c.username='******' "; $query_mid = ""; if ($this->id) { $query_mid = "WHERE id<" . $this->id . " "; } $query_end = "group by a.id ORDER BY a.id DESC LIMIT 10"; $query = $query_start . $query_mid . $query_end; $res = db::query($query); if ($res->rowCount() == 0) { return null; } $arr = $res->fetchAll(PDO::FETCH_ASSOC); $attachment = new ModelAttachment(array_values($arr)[0]['id'], null, null); $attch = $attachment->getAttachments(end($arr)['id']); foreach ($arr as $key => $row) { $arr[$key]['attachments'] = null; if ($attch !== null) { if (array_key_exists($row['id'], $attch)) { $arr[$key]['attachments'] = $attch[$row['id']]; } } } return $arr; }
public function testGetAttachmentNotExist() { $attach = new ModelAttachment(150, null, null); $actual = $attach->getAttachments(140); $expected = null; $this->assertEquals($expected, $actual); }