1

我有以下实体类和控制器类。我已经公开了选定的属性。

在我的“getAlbumsAction()”中,输出是我想要的方式。但在“getAlbumAction($id)”中,我想公开专辑类的所有属性。我怎样才能实现它?已经搜索了一段时间,但无法得到它。

<?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,
        );
    }

}
4

1 回答 1

0

你有一个错字:'album' => $entities但必须是 'album' => $entity 如果你想为一个对象返回不同的字段/格式,请查看 http://jmsyst.com/libs/serializer/主/参考/注释#groups

于 2013-09-16T12:53:48.800 回答