I am new to Symfony2. I am not using FOS UserBundle to implement security. Followed the Load user documentation in the main website of Symfony.
When I try to see the array of User entity it's showing empty for particular user as shown below
[roles:Acme\UserBundle\Entity\User:private] =>
Doctrine\Common\Collections\ArrayCollection Object (
[_elements:Doctrine\Common\Collections\ArrayCollection:private] =>
Array ( ) )
My User Entity class is
<?php
namespace Acme\UserBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Security\Core\User\UserInterface;
use Acme\UserBundle\Entity\UserRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Acme\UserBundle\Entity\Role;
/**
* Acme\Bundle\UserRegistrationBundle\Entity\User
*
* @ORM\Table(name="acme_users")
* @ORM\Entity(repositoryClass="Acme\UserBundle\Entity\UserRepository")
*/
class User implements UserInterface, \Serializable
{
/**
* @ORM\Column(type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORM\Column(type="string", length=25, unique=true)
*/
private $fname;
/**
* @ORM\Column(type="string", length=25, unique=true)
*/
private $lname;
/**
* @ORM\Column(type="string", length=60, unique=true)
*/
private $email;
/**
* @ORM\Column(type="string", length=64)
*/
private $password;
/**
* @ORM\Column(type="string", length=20)
*/
private $gender;
/**
* @ORM\Column(type="string", length=25, unique=true)
*/
private $profession;
/**
* @ORM\Column(type="date")
*/
private $date_of_birth;
/**
* @ORM\Column(type="integer")
*/
private $country_id;
/**
* @ORM\Column(type="integer")
*/
private $state_id;
/**
* @ORM\Column(type="integer")
*/
private $city_id;
/**
* @ORM\Column(type="string", length=20)
*/
private $phone_number;
/**
* @ORM\Column(name="status", type="boolean")
*/
private $status;
/**
* @ORM\Column(type="integer")
*/
private $is_lock;
/**
* @ORM\Column(type="integer")
*/
private $failed_attempt;
/**
* @ORM\Column(type="string", length=32)
*/
private $salt;
/**
* @ORM\ManyToMany(targetEntity="Role",inversedBy="users")
* @var ArrayCollection $roles;
*/
private $roles;
public function __construct()
{
$this->status = true;
$this->salt = md5(uniqid(null, true));
$this->roles = new ArrayCollection();
}
/**
* Get roles (array)
*
* @return array
*/
public function getRoles()
{
return $this->roles->toArray();
}
/**
* Get user_id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set fname
*
* @param string $fname
* @return User
*/
public function setFname($fname)
{
$this->fname = $fname;
return $this;
}
/**
* Get fname
*
* @return string
*/
public function getFname()
{
return $this->fname;
}
/**
* Set lname
*
* @param string $lname
* @return User
*/
public function setLname($lname)
{
$this->lname = $lname;
return $this;
}
/**
* Get lname
*
* @return string
*/
public function getLname()
{
return $this->lname;
}
/**
* Set email
*
* @param string $email
* @return User
*/
public function setEmail($email)
{
$this->email = $email;
return $this;
}
/**
* Get email
*
* @return string
*/
public function getEmail()
{
return $this->email;
}
/**
* Set password
*
* @param string $password
* @return User
*/
public function setPassword($password)
{
$this->password = $password;
return $this;
}
/**
* Get password
*
* @return string
*/
public function getPassword()
{
return $this->password;
}
/**
* Set gender
*
* @param string $gender
* @return User
*/
public function setGender($gender)
{
$this->gender = $gender;
return $this;
}
/**
* Get gender
*
* @return string
*/
public function getGender()
{
return $this->gender;
}
/**
* Set profession
*
* @param string $profession
* @return User
*/
public function setProfession($profession)
{
$this->profession = $profession;
return $this;
}
/**
* Get profession
*
* @return string
*/
public function getProfession()
{
return $this->profession;
}
/**
* Set date_of_birth
*
* @param \DateTime $dateOfBirth
* @return User
*/
public function setDateOfBirth($dateOfBirth)
{
$this->date_of_birth = $dateOfBirth;
return $this;
}
/**
* Get date_of_birth
*
* @return \DateTime
*/
public function getDateOfBirth()
{
return $this->date_of_birth;
}
/**
* Set country_id
*
* @param integer $countryId
* @return User
*/
public function setCountryId($countryId)
{
$this->country_id = $countryId;
return $this;
}
/**
* Get country_id
*
* @return integer
*/
public function getCountryId()
{
return $this->country_id;
}
/**
* Set state_id
*
* @param integer $stateId
* @return User
*/
public function setStateId($stateId)
{
$this->state_id = $stateId;
return $this;
}
/**
* Get state_id
*
* @return integer
*/
public function getStateId()
{
return $this->state_id;
}
/**
* Set city_id
*
* @param integer $cityId
* @return User
*/
public function setCityId($cityId)
{
$this->city_id = $cityId;
return $this;
}
/**
* Get city_id
*
* @return integer
*/
public function getCityId()
{
return $this->city_id;
}
/**
* Set phone_number
*
* @param string $phoneNumber
* @return User
*/
public function setPhoneNumber($phoneNumber)
{
$this->phone_number = $phoneNumber;
return $this;
}
/**
* Get phone_number
*
* @return string
*/
public function getPhoneNumber()
{
return $this->phone_number;
}
/**
* Set status
*
* @param boolean $status
* @return User
*/
public function setStatus($status)
{
$this->status = $status;
return $this;
}
/**
* Get status
*
* @return boolean
*/
public function getStatus()
{
return $this->status;
}
/**
* Set is_lock
*
* @param integer $isLock
* @return User
*/
public function setIsLock($isLock)
{
$this->is_lock = $isLock;
return $this;
}
/**
* Get is_lock
*
* @return integer
*/
public function getIsLock()
{
return $this->is_lock;
}
/**
* Set failed_attempt
*
* @param integer $failedAttempt
* @return User
*/
public function setFailedAttempt($failedAttempt)
{
$this->failed_attempt = $failedAttempt;
return $this;
}
/**
* Get failed_attempt
*
* @return integer
*/
public function getFailedAttempt()
{
return $this->failed_attempt;
}
/**
* Set salt
*
* @param string $salt
* @return User
*/
public function setSalt($salt)
{
$this->salt = $salt;
return $this;
}
/**
* Get salt
*
* @return string
*/
public function getSalt()
{
return $this->salt;
}
/**
* Add roles
*
* @param \Autograph\UserBundle\Entity\Role $roles
* @return User
*/
public function addRole(\Autograph\UserBundle\Entity\Role $roles)
{
$this->roles[] = $roles;
return $this;
}
/**
* Remove roles
*
* @param \Autograph\UserBundle\Entity\Role $roles
*/
public function removeRole(\Autograph\UserBundle\Entity\Role $roles)
{
$this->roles->removeElement($roles);
}
public function eraseCredentials() {
}
public function getUsername() {
}
public function serialize() {
}
public function unserialize($serialized) {
}
}
My Role Entity Class is
<?php
// src/Acme/UserBundle/Entity/Role.php
namespace Acme\UserBundle\Entity;
use Symfony\Component\Security\Core\Role\RoleInterface;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Table(name="acme_roles")
* @ORM\Entity()
*/
class Role implements RoleInterface
{
/**
* @ORM\Column(name="id", type="integer")
* @ORM\Id()
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORM\Column(name="name", type="string", length=30)
*/
private $name;
/**
* @ORM\Column(name="role", type="string", length=20, unique=true)
*
* @var string $role
*/
private $role;
/**
* @ORM\ManyToMany(targetEntity="User", mappedBy="roles")
*
* @var ArrayCollection $users
*/
private $users;
public function __construct()
{
$this->users = new ArrayCollection();
}
/**
* @see RoleInterface
*/
public function getRole()
{
return $this->role;
}
/**
* Set role
*
* @param string $role
* @return Role
*/
public function setRole($role)
{
$this->role = $role;
return $this;
}
// ... getters and setters for each property
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*
* @param string $name
* @return Role
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Add users
*
* @param \Autograph\UserBundle\Entity\User $users
* @return Role
*/
public function addUser(\Autograph\UserBundle\Entity\User $users)
{
$this->users[] = $users;
return $this;
}
/**
* Remove users
*
* @param \Autograph\UserBundle\Entity\User $users
*/
public function removeUser(\Autograph\UserBundle\Entity\User $users)
{
$this->users->removeElement($users);
}
/**
* Get users
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getUsers()
{
return $this->users;
}
}
What is wrong?