JMSSerializerbundle @expose relationship, ignores other entities policies

家住魔仙堡 提交于 2019-12-13 04:34:48

问题


I have 2 Entities.

A StockItem, and a User.

They look like this.

/**
 * StockItem
 *
 * @ORM\Table()
 * 
 * @ORM\Entity(repositoryClass="IREnterprise\AppBundle\Entity\StockItemRepository")
 * @ORM\HasLifecycleCallbacks
 *
 * @ExclusionPolicy("all")
 *
 */
class StockItem
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     * @Expose
     */
    private $id;

    /**
     * @ORM\ManyToOne(targetEntity="IREnterprise\UserBundle\Entity\User", inversedBy="stockItems")
     * @ORM\JoinColumn(name="user_id", referencedColumnName="id")
     * @Expose
     **/
    private $user;
    ...


/**
 * User
 *
 * @ORM\Table()
 * @ORM\Entity(repositoryClass="IREnterprise\UserBundle\Entity\UserRepository")
 *
 * @UniqueEntity("email")
 * @UniqueEntity("username")
 *
 * @ExclusionPolicy("all")
 *
 */
class User extends BaseUser
{

    const ROLE_CLIENT = 'ROLE_CLIENT';
    const ROLE_WORKER = 'ROLE_WORKER';

    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

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

As you see, both entities have ExclusionPolicty all, now if i perform a query on the StockItem, i get the full User object, the User objects own exclusion policies are ignored.

Even tho only 1 property, "company", is exposed within the User Entity.

Is it possible to @Expose a single property in the relationship? Without getting the entire object when exposing the relationship.


回答1:


Please have a look on JMSSerializer @Groups annotation instead (http://jmsyst.com/libs/serializer/master/cookbook/exclusion_strategies#creating-different-views-of-your-objects)

So you don't need to use ExclusionPolicy and Expose for your exclusion strategies



来源:https://stackoverflow.com/questions/31373667/jmsserializerbundle-expose-relationship-ignores-other-entities-policies

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