简体   繁体   English

防止 symfony 序列化程序到某些属性

[英]Prevent symfony serializer to certain attributes

I acknowledge of the existence of $normalizer->setIgnoredAttributes but I have the following problem.我承认 $normalizer->setIgnoredAttributes 的存在,但我有以下问题。

I have an entity Product with attributes 'prices' (related with another entity) and 'complements' (which is a self reference relation).我有一个具有属性“价格”(与另一个实体相关)和“补充”(这是一个自引用关系)的实体产品。 When I get the a product I need the prices, but when listing the complements, I don't need the prices in the complement product, is there any way to avoid getting the attribute prices only in the complements?当我得到一个产品时我需要价格,但是在列出补充时,我不需要补充产品中的价格,有什么方法可以避免只在补充中获得属性价格? Something like类似的东西

$normalizer->setIgnoredAttributes(array('complement->prices'));

There are several ways to accomplish this:有几种方法可以实现这一点:

  1. Use the Serializer annotations and specify different Serialization groups使用序列化器注释并指定不同的序列化组
  2. Use the CustomNormalizer and make your Product implement the NormalizableInterface使用 CustomNormalizer 并使您的产品实现NormalizableInterface
  3. Write a custom normalizer class which only supports you Product entity.编写一个仅支持您的Product实体的自定义规范器类。

The Serialization groups序列化组

By using annotations on each property of the Product entity you can specify if that property should be serialized or not, if it needs to be aliased or not, or if it belongs to one or more groups.通过在Product实体的每个属性上使用注释,您可以指定该属性是否应该序列化,是否需要别名,或者它是否属于一个或多个组。

When serializing you can then specify via the $context array which serialization group you want to serialize and the serializer will only serialize members of that group.序列化时,您可以通过$context数组指定要序列化的序列化组,序列化器只会序列化该组的成员。

The NormalizableInterface NormalizableInterface

By implementing NormalizableInterface in your Product entity you are passing the responsibility of normalization onto the entity itself.通过在您的Product实体中实现NormalizableInterface ,您将规范化的责任传递给实体本身。 It decides what the final normalized product will look like.它决定了最终标准化产品的外观。

By passing some information/flags in the $context array you ensure the normalization logic of the product entity will be aware if it's currently normalizing a standard product or a complement.通过在$context数组中传递一些信息/标志,您可以确保产品实体的规范化逻辑将知道它当前是规范化标准产品还是补充。

The custom normalizer class自定义规范器类

Without having to implement the NormalizableInterface on the entity your new normalizer class will only accept normalizing your Product entity (or whatever you decide to specify in supportsNormalization ).无需在实体上实现NormalizableInterface ,您的新规范器类将只接受规范化您的Product实体(或您决定在supportsNormalization指定的任何内容)。

The same $context logic must apply here as with the previous example.与前面的示例一样,此处必须应用相同的$context逻辑。

Should you ever need to completely exclude a property from being serialized, then since Symfony version 5.1 you have a much simpler option: the @Ignore annotation:如果您需要从序列化中完全排除某个属性,那么从 Symfony 5.1 版开始,您有一个更简单的选择: @Ignore注释:

For example:例如:

use Symfony\Component\Serializer\Annotation\Ignore;

class User
{
    public $login;
    public $email;

    /**
     * @Ignore
     */
    public $password;
}

And it works on access methods too:它也适用于访问方法:

class User
{
    // ...
    
    /**
     * @Ignore
     */
    public function getPassword(): string {}

    /**
     * @Ignore
     */
    public function isAdmin(): bool {}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM