我有一个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;
}
}
答案 0 :(得分: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