SF2 + FOSUserBundle + JMS 序列化器 - 扩展用户实体的字段未序列化

问题描述 投票:0回答:1

我有 symfony2 应用程序,其 fos-user-bundle 配置如下:

fos_user:
     db_driver: orm
     firewall_name: main
     user_class: AppBundle\Entity\User

我还使用 fos-rest-bundle,它使用 JMSSerializer 为我的 API 请求创建 JSON 响应。

我不明白为什么原始 FOS\UserBundle\Model\User 中的字段被序列化,而 AppBundle\Entity\User 中的额外字段被忽略

这是我的用户实体:

<?php

namespace AppBundle\Entity;

use FOS\UserBundle\Model\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;

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

/**
 * @Assert\NotBlank()
 */
protected $email;

/**
 * @Assert\NotBlank()
 */
protected $username;

/**
 * @var string
 * @ORM\Column(type="string", nullable=true)
 */
protected $phoneMobile;

/**
 * @var string
 * @ORM\Column(type="string", nullable=true)
 */
protected $phoneLandline;

/**
 * @var string
 * @ORM\Column(type="string", nullable=true)
 */
protected $skype;

public function __construct()
{
    parent::__construct();
}

function getPhoneMobile() {
    return $this->phoneMobile;
}

function getPhoneLandline() {
    return $this->phoneLandline;
}

function getSkype() {
    return $this->skype;
}

function setPhoneMobile($phoneMobile) {
    $this->phoneMobile = $phoneMobile;
}

function setPhoneLandline($phoneLandline) {
    $this->phoneLandline = $phoneLandline;
}

function setSkype($skype) {
    $this->skype = $skype;
}

public function serialize()
{
    return serialize(array(
        $this->password,
        $this->salt,
        $this->usernameCanonical,
        $this->username,
        $this->expired,
        $this->locked,
        $this->credentialsExpired,
        $this->enabled,
        $this->id,
        $this->expiresAt,
        $this->credentialsExpireAt,
        $this->email,
        $this->emailCanonical,
        $this->phoneMobile,
        $this->phoneLandline,
        $this->skype,
    ));
}

/**
 * Unserializes the user.
 *
 * @param string $serialized
 */
public function unserialize($serialized)
{
    $data = unserialize($serialized);
    // add a few extra elements in the array to ensure that we have enough keys when unserializing
    // older data which does not include all properties.
    $data = array_merge($data, array_fill(0, 2, null));

    list(
        $this->password,
        $this->salt,
        $this->usernameCanonical,
        $this->username,
        $this->expired,
        $this->locked,
        $this->credentialsExpired,
        $this->enabled,
        $this->id,
        $this->expiresAt,
        $this->credentialsExpireAt,
        $this->email,
        $this->emailCanonical,
        $this->phoneMobile,
        $this->phoneLandline,
        $this->skype
    ) = $data;
}
}
php symfony serialization fosuserbundle fosrestbundle
1个回答
0
投票

为了实现您想要的输出,创建serializer/fos/model.User.yml也使用注释组。

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;

use JMS\Serializer\Annotation\VirtualProperty;

use FOS\UserBundle\Model\User as BaseUser;
use JMS\Serializer\Annotation\ExclusionPolicy;
use JMS\Serializer\Annotation\Expose;
use JMS\Serializer\Annotation\Groups;
use JMS\Serializer\Annotation\MaxDepth;



/**
 * @ORM\Entity(repositoryClass="ProjectNAme\BundleBundle\DAO\UserRepository")
 * @ORM\Table(name="fos_user")
 */
class User extends BaseUser {
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     * @Groups({"group 1"})
     */
    protected $id;



    /**
    * @ORM\Column(type="string", nullable=true)
    * @Groups({"group1", "group2"})
    */
    private $nickname;

}

FOS\UserBundle\Model\User:
    exclusion_policy: ALL
    properties:
      id:
        expose: true
        groups: [group1]
      nickname:
        expose: true
        groups: [group1]
      attr2:
        expose: true
        groups: [group2]

您可以在此处阅读有关此主题的更多信息 https://github.com/schmittjoh/JMSSerializerBundle/issues/78#issuecomment-3851573

© www.soinside.com 2019 - 2024. All rights reserved.