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