从关系实体RestBundle Symfony2中排除某些属性

时间:2014-11-07 10:52:01

标签: php rest symfony fosrestbundle

当我返回一个带关系的对象时,我想要排除一些属性。

例如,我有一个实体用户和相册,我只想在获得相册列表时公开用户名。

有可能吗?

这是我的相册实体:

<?php

namespace Billion\AlbumBundle\Entity;

use Billion\AlbumBundle\Entity\Media;
use Doctrine\ORM\Mapping as ORM;

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

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

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

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

    /**
     * @ORM\OneToMany(targetEntity="Billion\AlbumBundle\Entity\Media", mappedBy="album")
     **/
    private $medias;

    /**
     * @ORM\OneToOne(targetEntity="Billion\UserBundle\Entity\Users")
     * @ORM\JoinColumn(name="owner_id", referencedColumnName="id", nullable=false)
     */
    private $owner;

    /**
     * @ORM\OneToOne(targetEntity="Billion\SecurityBundle\Entity\Visibility")
     * @ORM\JoinColumn(name="visibility_id", referencedColumnName="id", nullable=false)
     */
    private $visibility;


    /**
     * Constructor
     */
    public function __construct()
    {
        $this->medias = new \Doctrine\Common\Collections\ArrayCollection();
    }

}

我的用户实体:

namespace Billion\UserBundle\Entity;

use FOS\UserBundle\Model\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;

/**
 * Users
 *
 * @ORM\Table()
 * @ORM\Entity
 */
class Users extends BaseUser
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @ORM\OneToMany(targetEntity="Billion\UserBundle\Entity\Friends", mappedBy="myFriends")
     **/
    private $myFriends;


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

    /**
     * Add myFriends
     *
     * @param \Billion\UserBundle\Entity\Friends $myFriends
     * @return Users
     */
    public function addMyFriend(\Billion\UserBundle\Entity\Friends $myFriends)
    {
        $this->myFriends[] = $myFriends;

        return $this;
    }

    /**
     * Remove myFriends
     *
     * @param \Billion\UserBundle\Entity\Friends $myFriends
     */
    public function removeMyFriend(\Billion\UserBundle\Entity\Friends $myFriends)
    {
        $this->myFriends->removeElement($myFriends);
    }

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

以下是我对用户的排除政策:

Billion\AlbumBundle\Entity\Album:
    exclusion_policy: ALL
    properties:
        name:
            expose: true
        longitude:
            expose: true
        latitude:
            expose: true
        visibility:
            expose: true
        medias:
            expose: true
        owner:
            expose: true

感谢您的帮助!

1 个答案:

答案 0 :(得分:2)

我建议使用JMSSerializerBundle&#39; exclusion strategy for creating different views。这样,您可以根据视图对对象进行不同的序列化。因此,在您的情况下,您可以创建一个序列化相册的视图,该视图仅在用户对象时显示用户名。然后,您可以使用另一个视图来序列化完整的用户对象,以便在显示所有属性时使用。

以下是一个示例,我使用的是注释,但您也可以使用yaml配置。我也没有使用过FOSUserBundle,所以我的课程有点不同,但是它得到了重点。

class User implements AdvancedUserInterface
{
/**
 * @Groups({"user"})
 *
 * @ORM\Column(name="user_id", type="integer", nullable=false)
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="SEQUENCE")
 */
private $userId;

/**
 * @Groups({"user", "album"})
 * @ORM\Column(name="username", type="string", nullable=true)
 */
private $username;

/**
 * @Exclude
 * @ORM\Column(name="password", type="string", nullable=true)
 */
private $password;

// .......

}

在你的专辑课上,有:

class Album
{
/**
 * @var integer
 * @Groups({"album"})
 * @ORM\Column(name="id", type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 */
private $id;

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

// ......

}

以下是如何在控制器中使用它的示例(FOSRestBundle)

/**
 * Get currently logged in user
 */
public function getCurrentuserAction(){
    $loggedInUser = $this->get('security.context')->getToken()->getUser();

    $view = $this->view($loggedInUser , 200);

    $view->setSerializationContext(
        SerializationContext::create()->setGroups(array('user'))
    );
    return $this->handleView($view);
}

/**
 * Get album
 */
public function getCurrentuserAction(){
    $album = // get your album object

    $view = $this->view($album , 200);

    // set the group to only include the 'album' properties (including the 'album' marked username in the user object)
    $view->setSerializationContext(
        SerializationContext::create()->setGroups(array('album'))
    );
    return $this->handleView($view);
}

此方法非常灵活,您可以根据需要创建任意数量的组。