Symfony2 FosRestBundle在运行时公开属性

时间:2013-09-15 13:11:51

标签: php symfony fosrestbundle

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

在我的“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,
        );
    }

}

1 个答案:

答案 0 :(得分:0)

你有一个错字: 'album'=&gt; $ entities 但必须是'album'=&gt;的 $实体 如果你想为一个对象返回不同的字段/格式,请检查这个 http://jmsyst.com/libs/serializer/master/reference/annotations#groups