2015-10-23 66 views
-3

我想知道數據庫中用戶的角色時遇到問題。這是我的代碼:如何從數據庫中獲取用戶角色

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

我試圖讓這樣的角色:

$role = $user->getRoles(); 

,但它不能正常工作,並顯示錯誤:

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

更新:這裏 我代碼在實體/ 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; 
    } 
} 

幫我 感謝

+0

您應該爲我們提供更多與您的問題相關的代碼。 –

+0

我們還需要您的控制器的代碼。並在代碼中出現錯誤。 –

+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. 
+0

仍然無法正常工作並顯示錯誤消息:致命錯誤:調用成員函數getRoles()在非對象上 –

+0

這是另一個問題。你說玩具在數組轉換時遇到了問題。這個問題與你的'$ role'數組有關嗎?向我們提供完整的相關代碼和整個錯誤消息。 –

+0

好的。你有很多問題...將所有相關的代碼粘貼到原始文章中。然後人們將能夠幫助你。 –

相關問題