Пример #1
0
 public function getList()
 {
     $em = $this->getServiceLocator()->get('Doctrine\\ORM\\EntityManager');
     $albumRepo = $em->getRepository('\\Application\\Entity\\Album');
     $albums = $albumRepo->findAll();
     $collection = new \Doctrine\Common\Collections\ArrayCollection($albums);
     $data = $collection->map(function ($a) {
         // if this is a comment, map each element as an array, and the container as an array
         $a->comments = $a->comments->map(function ($p) {
             return $p->toArray();
         })->toArray();
         // map each of the elements within the collection as an array
         return $a->toArray();
     })->toArray();
     // result will stil be an array collection, that needs to be cast to an array
     return new JsonModel(array('data' => $data));
 }
 public function findAll()
 {
     $albums = $this->objectRepository->findAll();
     $collection = new \Doctrine\Common\Collections\ArrayCollection($albums);
     $arrays = $collection->map(function ($a) {
         $a->songs = $a->songs->map(function ($p) {
             return $p->toArray();
         })->toArray();
         return $a->toArray();
     })->toArray();
     //****************************************************************************************
     $fractal = new Manager();
     $resource = new Resource\Collection($arrays, function (array $array) {
         $songs = array();
         for ($i = 0; $i < count($array['songs']); $i++) {
             $songs[$i]['href'] = '/album/' . $array['id'] . '/' . $array['songs'][$i]['songTitle'];
         }
         return ['id' => (int) $array['id'], 'title' => $array['title'], 'artist' => $array['artist'], '_embedded' => ['songs' => $array['songs'], '_links' => ['rel' => $songs]], '_links' => [['rel' => 'self', 'uri' => '/albums/' . $array['id']]]];
     });
     $result = $fractal->createData($resource)->toArray();
     return $result;
 }