Domanda

I have following entity class and controller class. I have exposed selected properties.

In my "getAlbumsAction()" the output is the way i want. But in "getAlbumAction($id)" i want to expose all the properties of the album class. How can i achive it? Have been searching around for a while but can't get it.

<?php

namespace MyBundle\RestApiBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

use JMS\Serializer\Annotation\ExclusionPolicy;
use JMS\Serializer\Annotation\Expose;

/**
 * Album
 *
 * @ORM\Table()
 * @ORM\Entity(repositoryClass="MyBundle\RestApiBundle\Entity\AlbumRepository")
 * @ExclusionPolicy("all")
 */
class Album
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     * @expose
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\Column(name="name", type="string", length=255)
     * @expose
     */
    private $name;

    /**
     *
     * @ORM\OneToMany(targetEntity="Track", mappedBy="album")
     * 
     */
    private $tracks;

     /**
     *
     * @ORM\Column(name="likes", type="integer")
     * 
     * 
     */
    private $likes;

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="released", type="date")
     * 
     * 
     */

    private $released;

    /**
     * @var \DateTime
     * 
     * @ORM\Column(name="updated", type="date")
     */
    private $updated;

    /**
     * @ORM\ManyToOne(targetEntity="Language", inversedBy="albums")
     * @ORM\JoinColumn(name="language_id", referencedColumnName="id")
     *
     * 
     */
    private $language;


    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set name
     *
     * @param string $name
     * @return Album
     */
    public function setName($name)
    {
        $this->name = $name;

        return $this;
    }

    /**
     * Get name
     *
     * @return string 
     */
    public function getName()
    {
        return $this->name;
    }

    /**
     * Set released
     *
     * @param \DateTime $released
     * @return Album
     */
    public function setReleased($released)
    {
        $this->released = $released;

        return $this;
    }

    /**
     * Get released
     *
     * @return \DateTime 
     */
    public function getReleased()
    {
        return $this->released;
    }

    /**
     * Set updated
     *
     * @param \DateTime $updated
     * @return Album
     */
    public function setUpdated($updated)
    {
        $this->updated = $updated;

        return $this;
    }

    /**
     * Get updated
     *
     * @return \DateTime 
     */
    public function getUpdated()
    {
        return $this->updated;
    }

    /**
     * Set language
     *
     * @param integer $language
     * @return Album
     */
    public function setLanguage($language)
    {
        $this->language = $language;

        return $this;
    }

    /**
     * Get language
     *
     * @return integer 
     */
    public function getLanguage()
    {
        return $this->language;
    }
    /**
     * Constructor
     */
    public function __construct()
    {
        $this->tracks = new \Doctrine\Common\Collections\ArrayCollection();
    }

    /**
     * Add tracks
     *
     * @param \MyBundle\RestApiBundle\Entity\Track $tracks
     * @return Album
     */
    public function addTrack(\MyBundle\RestApiBundle\Entity\Track $tracks)
    {
        $this->tracks[] = $tracks;

        return $this;
    }

    /**
     * Remove tracks
     *
     * @param \MyBundle\RestApiBundle\Entity\Track $tracks
     */
    public function removeTrack(\MyBundle\RestApiBundle\Entity\Track $tracks)
    {
        $this->tracks->removeElement($tracks);
    }

    /**
     * Get tracks
     *
     * @return \Doctrine\Common\Collections\Collection 
     */
    public function getTracks()
    {
        return $this->tracks;
    }

    /**
     * Set likes
     *
     * @param integer $likes
     * @return Album
     */
    public function setLikes($likes)
    {
        $this->likes = $likes;

        return $this;
    }

    /**
     * Get likes
     *
     * @return integer 
     */
    public function getLikes()
    {
        return $this->likes;
    }
}



<?php

namespace MyBundle\RestApiBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use FOS\RestBundle\Controller\Annotations as Rest;

class AlbumController extends Controller {


    public function getAlbumsAction() {
        $em = $this->getDoctrine()->getManager();
        $entities = $em->getRepository('MyBundleRestApiBundle:Album')->findAll();
        return array(
            'albums' => $entities,
        );
    }

    private function getEntity($id) {
        $entityName = 'MyBundleRestApiBundle:Album';
        $em = $this->getDoctrine()->getManager();
        $entity = $em->getRepository($entityName)->find($id);
        if (!$entity) {
            throw $this->createNotFoundException('Unable to find organisation entity');
        }
        return $entity;
    }

    public function getAlbumAction($id) {
        $entity = $this->getEntity($id);
        return array(
            'album' => $entity,
        );
    }

}
È stato utile?

Soluzione

you have a typo: 'album' => $entities but must be 'album' => $entity also if you want to return different fields/formats for one object check this out http://jmsyst.com/libs/serializer/master/reference/annotations#groups

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top