0
votes

I have symfony2 application with fos-user-bundle configured like this:

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

I am also using fos-rest-bundle which is using JMSSerializer to create JSON response for my API requests.

I can't figure out why fields from original FOS\UserBundle\Model\User are serialized and my extra fields from AppBundle\Entity\User are ignored.

This is my user entity:

<?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;
}
}
1

1 Answers

0
votes

In order to achieve your desired output create serializer/fos/model.User.yml also use the annotation groups.

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]

You can read more on this topic at this https://github.com/schmittjoh/JMSSerializerBundle/issues/78#issuecomment-3851573