Exclude some attributes from relational entity RestBundle Symfony2

假装没事ソ 提交于 2019-12-25 03:13:30

问题


I want to exclude some attributes when I return an object with relations.

For example, I have an entity Users and Album and I just want to expose the username when I get a list of Album.

Is it possible ?

Here is my Album entity :

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

}

And my Users entity :

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;
    }
}

And here is my exclusion policy for Users :

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

Thank you for any help !


回答1:


I would recommend using JMSSerializerBundle's exclusion strategy for creating different views. This way you can have an object serialized differently depending on the view. So in your case, you could create a view for serializing Albums, which would only show the username when it comes to the user object. Then you could have another view for serializing your complete user object for when you want to show all properties.

Here's an example, I'm using annotations but you can use your yaml config as well. I also didn't use FOSUserBundle, so my class is a bit different, but it gets the point across.

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;

// .......

}

And in your album class, something like:

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;

// ......

}

Here is an example of how to use it in a controller (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);
}

This method is quite flexible, and you can create as many groups as you want.



来源:https://stackoverflow.com/questions/26799599/exclude-some-attributes-from-relational-entity-restbundle-symfony2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!