我想知道數據庫中用戶的角色時遇到問題。這是我的代碼:如何從數據庫中獲取用戶角色
$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;
}
}
幫我 感謝
您應該爲我們提供更多與您的問題相關的代碼。 –
我們還需要您的控制器的代碼。並在代碼中出現錯誤。 –
我仍然看不到你的控制器和你的錯誤出現的代碼和你的整個錯誤信息。 –