【问题标题】:Getting the group_id by using Groups with FOSUserBundle使用带有 FOSUserBundle 的 Groups 获取 group_id
【发布时间】:2013-05-11 17:47:32
【问题描述】:

我已经配置了 FOSUserBundle 组,关注Using Groups With 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

【问题讨论】:

    标签: symfony doctrine many-to-many fosuserbundle


    【解决方案1】:

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

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

    另外一个在你的Group 实体中如下:

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

    $this->groups 是您的User 实体中的ArrayCollection 对象,您可以这样做:

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

    【讨论】:

    • 感谢您的回复,我作为跟随指南并发生错误“无法将数据库值 \"Hello\" 转换为 Doctrine Type 数组”
    【解决方案2】:

    谢谢cheesemacfly!...我的方法之一

    用户实体

    命名空间 Hta\CoreBundle\Entity;

    使用 Doctrine\ORM\Mapping 作为 ORM;

    /** * HtaUser / class HtaUser { /* * @var 字符串 */ 私人$用户名;

    /**
     * @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 行中 5 个字节的偏移量 0 处出错

    print_r $user 对象

    [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 对象 递归 )

    【讨论】:

    • 打印组:echo '
      '; print_r(\Doctrine\Common\Util\Debug::dump($user->getGroup()));我打错了,角色列使用 DC2Type:array
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-07-25
    • 2016-06-02
    • 1970-01-01
    • 2020-04-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多