em = $em; $this->hydrator = $hydrator; } public function getList() { $qb = $this->em->createQueryBuilder(); $entities = $qb->select('c,u,a') ->from(Comment::class, 'c') ->leftJoin('c.author', 'u') ->leftJoin('c.article', 'a') ->getQuery() ->getArrayResult(); return $entities; } public function get($id) { $qb = $this->em->createQueryBuilder(); $entity = $qb->select('c,u,a') ->from(Comment::class, 'c') ->leftJoin('c.author', 'u') ->leftJoin('c.article', 'a') ->where('c.id = :cid') ->setParameter('cid', $id) ->getQuery() ->getOneOrNullResult(Query::HYDRATE_ARRAY); return $entity; } public function create($data) { $entity = $this->hydrator->hydrate($data, new Comment()); try { $this->em->persist($entity); $this->em->flush(); } catch (Exception $ex) { return false; } return $entity; } public function modify($id, $data) { if (null === ($entity = $this->em->find(Comment::class, $id))) { return false; } $entity = $this->hydrator->hydrate($data, $entity); $this->em->persist($entity); $this->em->flush(); return $entity; } public function delete($id) { if (null === ($entity = $this->em->find(Comment::class, $id))) { return false; } try { $this->em->remove($entity); $this->em->flush(); } catch (Exception $ex) { return false; } return true; } }