Php Symfony2-无法将角色分配给用户(角色数组为空)

Php Symfony2-无法将角色分配给用户(角色数组为空),php,arrays,database,symfony,doctrine-orm,Php,Arrays,Database,Symfony,Doctrine Orm,我是新来的Symfony2。我没有使用FOS UserBundle来实现安全性。 遵循Symfony主网站上的加载用户文档 当我尝试查看用户实体的数组时,对于特定用户,它显示为空,如下所示 [roles:Acme\UserBundle\Entity\User:private] => Doctrine\Common\Collections\ArrayCollection Object ( [_elements:Doctrine\Comm

我是新来的Symfony2。我没有使用FOS UserBundle来实现安全性。 遵循Symfony主网站上的加载用户文档

当我尝试查看用户实体的数组时,对于特定用户,它显示为空,如下所示

    [roles:Acme\UserBundle\Entity\User:private] =>
            Doctrine\Common\Collections\ArrayCollection Object (
            [_elements:Doctrine\Common\Collections\ArrayCollection:private] =>
                 Array ( ) ) 
我的用户实体类是

<?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) {


}
}

向我们展示您为用户创建/设置角色的控制器(避免“非常紧急”…)。我正在直接从phpmyadmin添加角色,关于那里的id(好的,我不会使用它),在我看来,您的实体角色中的属性用户是无用的。此外,如何在MySQL中插入数据?因为MySQL中不存在数组。也许在科幻小说中更容易做到,使用形式,教义等等。我不知道有什么不对。
$this->getUser()的返回值是多少在控制器中?请停止为同一问题打开新问题!如果要在问题中添加内容,请编辑现有问题,而不是打开新问题。现在,在几个小时内,您已经打开了三个重复的问题:,和。
<?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;
}
}