如何防止 doctrine2 返回某些字段


How to to prevent doctrine2 from returning certain fields

我编码的是与教义的一对一关系

一个用户--->有多个通知

这就是我获取数据的方式

/**
 * @Route("/test")
 */
public function testRoute()
{
    //get the user notifications 
    $notifications = $this->getUser()->getNotifications();
    //return json response
    $serializer = $this->get('serializer');
    $json = $serializer->serialize($notifications, 'json');
    $response =  new Response($json);
    $response->headers->set('Content-Type', 'application/json');
    return $response;
}

这是控制器返回的内容

    [
{
    "id": 1,
    "receiver": 1,
    "notification_type": "new_comment",
    "triggered_by": {
      "id": 1,
      "username": "gabriel",
      "username_canonical": "gabriel",
      "password":    "3e6bS2I==",
      "email": "ga@ga.de",
      "first_name": "Gabriel",
      "last_name": "ThaKid",
      "likes_counter": 0,
      "dislikes_counter": 2,
      "favourites_counter": 0,
      "profile_pic": "profilepic_60181.png", 
      "salt": "Lqch0N84UH1QmFI5O",
      "form_token": "sO6NgWd",
      "is_active": true, 
      "registration_confirmation": "success",
      "secret_confirmation_id": "qTwNGm4CSKHzJOe8ry9DcXavt",
      "socket_token": "KuMlxYHa"
},
"created_at": "2014-12-16T13:36:20+0100",
"link_to": "#test"
},
{
"id": 2,
"receiver": 1,
"notification_type": "new_comment",
 "triggered_by": {
   "id": 1,
   "username": "gabriel",
   "username_canonical": "gabriel",
   "password": "3e6bS2IYX1DONLA/70a8hzMUQ==",
   "email": "ga@ga.de",
   "first_name": "Gabriel",
   "last_name": "ThaKid",
   "likes_counter": 0,
   "dislikes_counter": 2,
   "favourites_counter": 0,
   "profile_pic": "profilepic_60181.png",
   "profile_rank": "Beginner", (...)
},
   "created_at": "2014-12-16T13:36:24+0100",
   "link_to": "#test"
}
]

我想你明白了,它返回某个用户的通知,这很好,我还需要用户的某些字段,例如姓氏和名字,以便返回的数据可在应用程序中使用。但 doctrine 还返回散列密码和盐以及用户不需要知道的令牌和信息。

我如何告诉教义不要返回这些字段或不获取它们?

这与 Doctrine 无关,这是尝试获取并返回所有可用值的默认Serializer

查看序列化程序组件文档,了解如何忽略属性。基本上,您必须将规范化器传递到序列化器的构造函数中:

<?php
use Symfony'Component'Serializer'Serializer;
use Symfony'Component'Serializer'Encoder'JsonEncoder;
use Symfony'Component'Serializer'Normalizer'GetSetMethodNormalizer;
$normalizer = new GetSetMethodNormalizer();
$normalizer->setIgnoredAttributes(array('age'));
$encoder = new JsonEncoder();
$serializer = new Serializer(array($normalizer), array($encoder));
$serializer->serialize($person, 'json');

另外,我强烈建议切换到JMSSerializer和/或使用FOSRestBundle,因为它为序列化提供了更大的灵活性。属性列表是针对上下文配置的,并具有基本排除策略。这样,您只需列出要公开的属性,而不是排除:

AppBundle'Entity'Car:
  exclusion_policy: all
  properties:
    id:
      expose: true
    vendor:
      expose: true
    title:
      expose: true

在给定的示例中,将排除所有未列出的属性。

您应该创建自定义存储库类。

在此类中,创建一个如下所示的方法:

// src/AppBundle/Entity/NotificationRepository.php
namespace AppBundle'Entity;
use Doctrine'ORM'EntityRepository;
class NotificationRepository extends EntityRepository
{
    public function findAllByUser()
    {
        return $this->getEntityManager()
            ->createQuery(
                'SELECT [only the fields you need] FROM [....]'
            )
            ->getResult();
    }
}

对于 DQL 查询,可以使用部分对象语法。