如何从数据库中获取用户角色

时间:2015-10-23 03:41:19

标签: symfony

当我想知道数据库中用户的角色时,我遇到了问题。这是我的代码:

$user = $this->getDoctrine()
  ->getRepository('DSBBundle:User')
->findOneBy(array('email' => $email));

我尝试得到这样的角色:

$role = $user->getRoles();

但它不起作用并显示错误:

Notice: array to string conversion in .....

更新: 这里我的代码在Entity / User.php

<?php

namespace DSBBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Security\Core\User\AdvancedUserInterface;
use Doctrine\Common\Collections\ArrayCollection;

//http://symfony.com/doc/current/book/security.html#book-security-encoding-user-password
//password_hash('ryanpass', PASSWORD_BCRYPT, array('cost' => 12));
/*
$factory = $this->get('security.encoder_factory');
$user = new Acme\UserBundle\Entity\User();

$encoder = $factory->getEncoder($user);
$password = $encoder->encodePassword('ryanpass', $user->getSalt());
$user->setPassword($password);
 */
// * @ORM\Table(name="dbo.[User]")

/**
 * @ORM\Entity(repositoryClass="DSBBundle\Entity\UserRepository")
 */
class User implements AdvancedUserInterface, \Serializable
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    public $id;

    /**
     * @ORM\Column(type="string", length=255, unique=true, options={"label" = "Username"})
     * @Assert\NotBlank
     */
    public $username;

    /**
     * @ORM\Column(type="string", length=255)
     */
    public $password;

    /**
     * @ORM\Column(type="string", length=255, options={"label" = "Name"})
     * @Assert\NotBlank
     */
    public $name;

    /**
     * @ORM\Column(type="string", length=255, nullable=true, options={"label" = "Email"})
     */
    private $email;

    /**
     * @ORM\Column(name="is_active", type="boolean", options={"label" = "Active"})
     */
    private $isActive;

    /**
     * @ORM\ManyToMany(targetEntity="Role", inversedBy="users")
     *
     */
    private $roles;

    /**
     * @ORM\ManyToMany(targetEntity="School", inversedBy="users")
     */
    protected $schools;

//=====

    public function __construct()
    {
        $this->roles = new ArrayCollection();
    }

    public function getRoles()
    {
        return $this->roles->toArray();
    }

    public function isAccountNonExpired()
    {
        return true;
    }

    public function isAccountNonLocked()
    {
        return true;
    }

    public function isCredentialsNonExpired()
    {
        return true;
    }

    public function isEnabled()
    {
        return $this->isActive;
    }

    /**
     * @inheritDoc
     */
    public function getSalt()
    {
        // you *may* need a real salt depending on your encoder
        // see section on salt below
        return null;
    }

    /**
     * @inheritDoc
     */
    public function eraseCredentials()
    {
    }

    /**
     * @see \Serializable::serialize()
     */
    public function serialize()
    {
        return serialize(array(
            $this->id,
            $this->username,
            $this->password
            //$this->salt,
        ));
    }

    /**
     * @see \Serializable::unserialize()
     */
    public function unserialize($serialized)
    {
        list (
            $this->id,
            $this->username,
            $this->password
            //$this->salt
        ) = unserialize($serialized);
    }

//=======    

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

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

        return $this;
    }

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

    /**
     * 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 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 isActive
     *
     * @param boolean $isActive
     *
     * @return User
     */
    public function setIsActive($isActive)
    {
        $this->isActive = $isActive;

        return $this;
    }

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

    /**
     * Add roles
     *
     * @param \DSBBundle\Entity\Role $roles
     *
     * @return User
     */
    public function addRole(\DSBBundle\Entity\Role $roles)
    {
        $this->roles[] = $roles;

        return $this;
    }

    /**
     * Remove roles
     *
     * @param \DSBBundle\Entity\Role $roles
     */
    public function removeRole(\DSBBundle\Entity\Role $roles)
    {
        $this->roles->removeElement($roles);
    }

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

        return $this;
    }

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


    /**
     * Add school
     *
     * @param \DSBBundle\Entity\School $school
     *
     * @return User
     */
    public function addSchool(\DSBBundle\Entity\School $school)
    {
        $this->schools[] = $school;

        return $this;
    }

    /**
     * Remove school
     *
     * @param \DSBBundle\Entity\School $school
     */
    public function removeSchool(\DSBBundle\Entity\School $school)
    {
        $this->schools->removeElement($school);
    }

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

帮帮我 感谢

1 个答案:

答案 0 :(得分:0)

如错误消息中所述,您尝试将roles ArrayCollection /数组转换为字符串。这是不对的。您应该像使用任何其他数组或ArrayCollection一样使用roles ArrayCollection。例如:要获取ArrayCollection的第一个元素,您可以使用下一个代码:

$primaryRole = $user->getRoles()->first();
// or
$roles = $user->getRoles();
$primaryRole = $roles[0]; // but in this case it will throw you undefined key Exception if you will have empty array.