简单的过程, 需要创建具有映射角色的用户。
我按照链接的步骤进行操作 http://symfony.com/doc/current/cookbook/security/entity_provider.html
生成了用户和角色表但在MySql中未生成users_roles表... 我需要手动创建吗?
二 我已使用用户表进行身份验证
登录后,它会重定向到错误页面
FatalErrorException: Error: Call to a member function toArray() on a non-object in /var/www/vibilling_3/src/ViBillingPortal/AuthenticationBundle/Entity/users.php line 130
我搜索过,但我找不到任何解决方案......在我的代码下面
比尔/ PortalBundle /实体/ users.php
namespace ViBillingPortal\AuthenticationBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Security\Core\User\UserInterface;
use Doctrine\Common\Collections\ArrayCollection;
/**
* users
*/
class users implements UserInterface, \Serializable
{
/**
* @var integer
*/
private $id;
/**
* @var string
*/
private $username;
/**
* @var string
*/
private $password;
/**
* @var string
*/
private $created_date;
/**
* @ORM\ManyToMany(targetEntity="roles", inversedBy="users")
* @ORM\JoinTable(name="user_roles")
*/
private $userroles;
public function __construct()
{
$this->userroles = new ArrayCollection();
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set username
*
* @param string $username
* @return users
*/
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 users
*/
public function setPassword($password)
{
$this->password = $password;
return $this;
}
/**
* Get password
*
* @return string
*/
public function getPassword()
{
return $this->password;
}
/**
* Set created_date
*
* @param string $created_date
* @return users
*/
public function setCreated_date($password)
{
$this->password = $created_date;
return $this;
}
/**
* Get Created_date
*
* @return string
*/
public function getCreated_date()
{
return $this->created_date;
}
/**
* Get Roles
*/
public function getRoles()
{
return $this->userroles->toArray();
}
/**
* @inheritDoc
*/
public function getSalt()
{
}
/**
* @inheritDoc
*/
public function eraseCredentials()
{
}
/**
* @see \Serializable::serialize()
*/
public function serialize()
{
}
/**
* @see \Serializable::unserialize()
*/
public function unserialize($serialized)
{
}
}
比尔/ PortalBundle /实体/ roles.php
namespace ViBillingPortal\AuthenticationBundle\Entity;
use Symfony\Component\Security\Core\Role\RoleInterface;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
/**
* roles
*/
class roles implements RoleInterface, \Serializable
{
/**
* @var integer
*/
private $id;
/**
* @var string
*/
private $name;
/**
* @ORM\Column(name="role", type="string", length=20, unique=true)
*/
private $role;
/**
* @ORM\ManyToMany(targetEntity="users", mappedBy="userroles")
*/
protected $users;
public function __construct()
{
$this->users = new ArrayCollection();
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*
* @param string $name
* @return roles
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* @see RoleInterface
*/
public function getRole()
{
return $this->role;
}
/**
* @see \Serializable::serialize()
*/
public function serialize()
{
}
/**
* @see \Serializable::unserialize()
*/
public function unserialize($serialized)
{
}
}
答案 0 :(得分:0)
如果要使用连接表,则应使用ManyToMany关系,而不是ManyToOne:http://docs.doctrine-project.org/en/2.0.x/reference/association-mapping.html#many-to-many-bidirectional
对于第二个错误,在构造方法中将usersroles初始化为ArrayCollection时很奇怪,它应该可以工作。 你能添加一个var_dump来查看这个属性中存储的内容吗?
为什么不为userroles获取任何setter / getter?
我认为您还应该阅读Symfony编码标准:http://symfony.com/doc/current/contributing/code/standards.html。你的编码风格不一致。