通过使用具有FOSUserBundle的组获取group_id

时间:2013-05-11 17:47:32

标签: symfony doctrine many-to-many fosuserbundle

我已按照Using Groups With FOSUserBundle配置了FOSUserBundle群组,并让它发挥作用。

// src/SM4/UserBundle/Entity/User.php 
/**
 * @ORM\ManyToMany(targetEntity="SM4\UserBundle\Entity\Group")
 * @ORM\JoinTable(name="sm4_user_group",
 *      joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")},
 *      inverseJoinColumns={@ORM\JoinColumn(name="group_id", referencedColumnName="id")}
 * )
 */
 protected $groups;

每次创建新User时,我都可以使用:

$userObj = new \SM4\UserBundle\Entity\User;

$userObj->getId();

$userObj->getEmail();

....

$userObj->getGroup();

但是如何获得用户的Group_id

2 个答案:

答案 0 :(得分:0)

假设您的User实体中有以下功能:

public function getGroups()
{
    return $this->groups;
}

以及Group实体中的另一个:

public function getId()
{
    return $this->id;
}

并且$this->groupsArrayCollection实体中的User对象,您可以这样做:

foreach ($userObj->getGroups() as $group)
{
    //this is where you get your groups id
    echo $group->getId();
}

答案 1 :(得分:0)

感谢cheesemacfly!...我的一种方式

用户实体

  

     

命名空间Hta \ CoreBundle \ Entity;

     

将Doctrine \ ORM \ Mapping用作ORM;

     

/ ** * HtaUser / class HtaUser {       / *        * @var字符串        * /       private $ username;

/**
 * @var string
 */
private $usernameCanonical;

/**
 * @var string
 */
private $email;

/**
 * @var string
 */
private $emailCanonical;

/**
 * @var boolean
 */
private $enabled;

/**
 * @var string
 */
private $salt;

/**
 * @var string
 */
private $password;

/**
 * @var \DateTime
 */
private $lastLogin;

/**
 * @var boolean
 */
private $locked;

/**
 * @var boolean
 */
private $expired;

/**
 * @var \DateTime
 */
private $expiresAt;

/**
 * @var string
 */
private $confirmationToken;

/**
 * @var \DateTime
 */
private $passwordRequestedAt;

/**
 * @var array
 */
private $roles;

/**
 * @var boolean
 */
private $credentialsExpired;

/**
 * @var \DateTime
 */
private $credentialsExpireAt;

/**
 * @var integer
 */
private $id;

/**
 * @var \Doctrine\Common\Collections\Collection
 */
private $group;

/**
 * Constructor
 */
public function __construct()
{
    $this->group = new \Doctrine\Common\Collections\ArrayCollection();
}

/**
 * Set username
 *
 * @param string $username
 * @return HtaUser
 */
public function setUsername($username)
{
    $this->username = $username;

    return $this;
}

/**
 * Get username
 *
 * @return string 
 */
public function getUsername()
{
    return $this->username;
}

/**
 * Set usernameCanonical
 *
 * @param string $usernameCanonical
 * @return HtaUser
 */
public function setUsernameCanonical($usernameCanonical)
{
    $this->usernameCanonical = $usernameCanonical;

    return $this;
}

/**
 * Get usernameCanonical
 *
 * @return string 
 */
public function getUsernameCanonical()
{
    return $this->usernameCanonical;
}

/**
 * Set email
 *
 * @param string $email
 * @return HtaUser
 */
public function setEmail($email)
{
    $this->email = $email;

    return $this;
}

/**
 * Get email
 *
 * @return string 
 */
public function getEmail()
{
    return $this->email;
}

/**
 * Set emailCanonical
 *
 * @param string $emailCanonical
 * @return HtaUser
 */
public function setEmailCanonical($emailCanonical)
{
    $this->emailCanonical = $emailCanonical;

    return $this;
}

/**
 * Get emailCanonical
 *
 * @return string 
 */
public function getEmailCanonical()
{
    return $this->emailCanonical;
}

/**
 * Set enabled
 *
 * @param boolean $enabled
 * @return HtaUser
 */
public function setEnabled($enabled)
{
    $this->enabled = $enabled;

    return $this;
}

/**
 * Get enabled
 *
 * @return boolean 
 */
public function getEnabled()
{
    return $this->enabled;
}

/**
 * Set salt
 *
 * @param string $salt
 * @return HtaUser
 */
public function setSalt($salt)
{
    $this->salt = $salt;

    return $this;
}

/**
 * Get salt
 *
 * @return string 
 */
public function getSalt()
{
    return $this->salt;
}

/**
 * Set password
 *
 * @param string $password
 * @return HtaUser
 */
public function setPassword($password)
{
    $this->password = $password;

    return $this;
}

/**
 * Get password
 *
 * @return string 
 */
public function getPassword()
{
    return $this->password;
}

/**
 * Set lastLogin
 *
 * @param \DateTime $lastLogin
 * @return HtaUser
 */
public function setLastLogin($lastLogin)
{
    $this->lastLogin = $lastLogin;

    return $this;
}

/**
 * Get lastLogin
 *
 * @return \DateTime 
 */
public function getLastLogin()
{
    return $this->lastLogin;
}

/**
 * Set locked
 *
 * @param boolean $locked
 * @return HtaUser
 */
public function setLocked($locked)
{
    $this->locked = $locked;

    return $this;
}

/**
 * Get locked
 *
 * @return boolean 
 */
public function getLocked()
{
    return $this->locked;
}

/**
 * Set expired
 *
 * @param boolean $expired
 * @return HtaUser
 */
public function setExpired($expired)
{
    $this->expired = $expired;

    return $this;
}

/**
 * Get expired
 *
 * @return boolean 
 */
public function getExpired()
{
    return $this->expired;
}

/**
 * Set expiresAt
 *
 * @param \DateTime $expiresAt
 * @return HtaUser
 */
public function setExpiresAt($expiresAt)
{
    $this->expiresAt = $expiresAt;

    return $this;
}

/**
 * Get expiresAt
 *
 * @return \DateTime 
 */
public function getExpiresAt()
{
    return $this->expiresAt;
}

/**
 * Set confirmationToken
 *
 * @param string $confirmationToken
 * @return HtaUser
 */
public function setConfirmationToken($confirmationToken)
{
    $this->confirmationToken = $confirmationToken;

    return $this;
}

/**
 * Get confirmationToken
 *
 * @return string 
 */
public function getConfirmationToken()
{
    return $this->confirmationToken;
}

/**
 * Set passwordRequestedAt
 *
 * @param \DateTime $passwordRequestedAt
 * @return HtaUser
 */
public function setPasswordRequestedAt($passwordRequestedAt)
{
    $this->passwordRequestedAt = $passwordRequestedAt;

    return $this;
}

/**
 * Get passwordRequestedAt
 *
 * @return \DateTime 
 */
public function getPasswordRequestedAt()
{
    return $this->passwordRequestedAt;
}

/**
 * Set roles
 *
 * @param array $roles
 * @return HtaUser
 */
public function setRoles($roles)
{
    $this->roles = $roles;

    return $this;
}

/**
 * Get roles
 *
 * @return array 
 */
public function getRoles()
{
    return $this->roles;
}

/**
 * Set credentialsExpired
 *
 * @param boolean $credentialsExpired
 * @return HtaUser
 */
public function setCredentialsExpired($credentialsExpired)
{
    $this->credentialsExpired = $credentialsExpired;

    return $this;
}

/**
 * Get credentialsExpired
 *
 * @return boolean 
 */
public function getCredentialsExpired()
{
    return $this->credentialsExpired;
}

/**
 * Set credentialsExpireAt
 *
 * @param \DateTime $credentialsExpireAt
 * @return HtaUser
 */
public function setCredentialsExpireAt($credentialsExpireAt)
{
    $this->credentialsExpireAt = $credentialsExpireAt;

    return $this;
}

/**
 * Get credentialsExpireAt
 *
 * @return \DateTime 
 */
public function getCredentialsExpireAt()
{
    return $this->credentialsExpireAt;
}

/**
 * Get id
 *
 * @return integer 
 */
public function getId()
{
    return $this->id;
}

/**
 * Add group
 *
 * @param \Hta\CoreBundle\Entity\HtaGroup $group
 * @return HtaUser
 */
public function addGroup(\Hta\CoreBundle\Entity\HtaGroup $group)
{
    $this->group[] = $group;

    return $this;
}

/**
 * Remove group
 *
 * @param \Hta\CoreBundle\Entity\HtaGroup $group
 */
public function removeGroup(\Hta\CoreBundle\Entity\HtaGroup $group)
{
    $this->group->removeElement($group);
}

/**
 * Get group
 *
 * @return \Doctrine\Common\Collections\Collection 
 */
public function getGroup()
{
    return $this->group;
} }

集团实体

  

     

命名空间Hta \ CoreBundle \ Entity;

     

将Doctrine \ ORM \ Mapping用作ORM;

     

/ ** * HtaGroup / class HtaGroup {       / *        * @var字符串        * /       私人$ name;

/**
 * @var array
 */
private $roles;

/**
 * @var integer
 */
private $id;

/**
 * @var \Doctrine\Common\Collections\Collection
 */
private $user;

/**
 * Constructor
 */
public function __construct()
{
    $this->user = new \Doctrine\Common\Collections\ArrayCollection();
}

/**
 * Set name
 *
 * @param string $name
 * @return HtaGroup
 */
public function setName($name)
{
    $this->name = $name;

    return $this;
}

/**
 * Get name
 *
 * @return string 
 */
public function getName()
{
    return $this->name;
}

/**
 * Set roles
 *
 * @param array $roles
 * @return HtaGroup
 */
public function setRoles($roles)
{
    $this->roles = $roles;

    return $this;
}

/**
 * Get roles
 *
 * @return array 
 */
public function getRoles()
{
    return $this->roles;
}

/**
 * Get id
 *
 * @return integer 
 */
public function getId()
{
    return $this->id;
}

/**
 * Add user
 *
 * @param \Hta\CoreBundle\Entity\HtaUser $user
 * @return HtaGroup
 */
public function addUser(\Hta\CoreBundle\Entity\HtaUser $user)
{
    $this->user[] = $user;

    return $this;
}

/**
 * Remove user
 *
 * @param \Hta\CoreBundle\Entity\HtaUser $user
 */
public function removeUser(\Hta\CoreBundle\Entity\HtaUser $user)
{
    $this->user->removeElement($user);
}

/**
 * Get user
 *
 * @return \Doctrine\Common\Collections\Collection 
 */
public function getUser()
{
    return $this->user;
} }
$user = $em -> getRepository('HtaCoreBundle:HtaUser' ) -> find($id);

foreach ($user->getGroup() as $group)
{
    //this is where you get your groups id
    echo $group->getId();
}

注意:unserialize():D:\ symfony \ vendor \ doctrine \ dbal \ lib \ Doctrine \ DBAL \ Types \ ArrayType.php第48行中偏移量为0的5字节错误

print_r $ user object

[owner:Doctrine\ORM\PersistentCollection:private] => Hta\CoreBundle\Entity\HtaUser Object
        (
        [username:Hta\CoreBundle\Entity\HtaUser:private] => admin
        [usernameCanonical:Hta\CoreBundle\Entity\HtaUser:private] => admin
        [email:Hta\CoreBundle\Entity\HtaUser:private] => admin@yahoo.com
        [emailCanonical:Hta\CoreBundle\Entity\HtaUser:private] => admin@yahoo.com
        [enabled:Hta\CoreBundle\Entity\HtaUser:private] => 1
        [salt:Hta\CoreBundle\Entity\HtaUser:private] => 
        [password:Hta\CoreBundle\Entity\HtaUser:private] => 
        [lastLogin:Hta\CoreBundle\Entity\HtaUser:private] => 
        [locked:Hta\CoreBundle\Entity\HtaUser:private] => 
        [expired:Hta\CoreBundle\Entity\HtaUser:private] => 
        [expiresAt:Hta\CoreBundle\Entity\HtaUser:private] => 
        [confirmationToken:Hta\CoreBundle\Entity\HtaUser:private] => 
        [passwordRequestedAt:Hta\CoreBundle\Entity\HtaUser:private] => 
        [roles:Hta\CoreBundle\Entity\HtaUser:private] => Array
            (
                [0] => ROLE_ADMIN
            )

        [credentialsExpired:Hta\CoreBundle\Entity\HtaUser:private] => 
        [credentialsExpireAt:Hta\CoreBundle\Entity\HtaUser:private] => 
        [id:Hta\CoreBundle\Entity\HtaUser:private] => 2
        [group:Hta\CoreBundle\Entity\HtaUser:private] => Doctrine\ORM\PersistentCollection Object

递推         )

如何打印连接表输出? ([group:Hta \ CoreBundle \ Entity \ HtaUser:private] => Doctrine \ ORM \ PersistentCollection对象  递推         )