0

我已经像这样配置了 JMS 序列化器:

jms_serializer:
    visitors:
        xml_serialization:
            format_output: '%kernel.debug%'
    metadata:
        auto_detection: true
        directories:
            App:
                namespace_prefix: "App\\Entity"
                path: "%kernel.root_dir%/serializer"

这是 src/serializer/SystemUser.yml 中元数据配置对于实体 SystemUser 的样子:

App\Entity\Api\Auth\SystemUser:
    exclusion_policy: ALL
    properties:
        id:
            expose: true
        password:
            expose: false
        username:
            expose: true
        email:
            expose: true
        last_login:
            expose: true

我专门使用了 .yml 扩展名,因为文档中提到必须在此处使用 .yml 扩展名:

https://jmsyst.com/bundles/JMSSerializerBundle/2.x/configuration#defining-metadata

我在 src/Entity/Api/Auth/SystemUser.php 中有如下实体:

<?php

namespace App\Entity\Api\Auth;

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

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

    public function __construct()
    {
        parent::__construct();
        // your own logic
    }
}

但是无论我在 SystemUser.yml 文件中排除什么属性,输出都不会改变。它显示所有属性。

我正在使用最新的 Symfony 4.3

4

1 回答 1

1

您的文件src/serializer/SystemUser.yml名称错误。和namespace_prefix: "App\Entity"你的SystemUser.php命名空间应该是Api.Auth.SystemUser.yml

这是因为您的用户扩展了 BaseUser。你需要这样做:

jms_serializer.yaml

jms_serializer:
    metadata:
        auto_detection: true
        directories:
            App:
                namespace_prefix: 'App\Entity'
                path: '%kernel.root_dir%/serializer'
            FOSUB:
                namespace_prefix: 'FOS\UserBundle'
                path: '%kernel.root_dir%/serializer'

src/serializer/Model.User.yml

FOS\UserBundle\Model\User:
    exclusion_policy: ALL
    properties:
        id:
            exclude: false
        username:
            exclude: false
于 2019-08-27T16:38:16.060 回答