gpt4 book ai didi

php - 创建用户角色 Symfony2 的问题

转载 作者:行者123 更新时间:2023-11-30 23:08:21 25 4
gpt4 key购买 nike

简单的过程,需要创建具有映射角色的用户。

我按照链接中的步骤操作 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

我搜索过,但找不到任何解决方案...在我的代码下方

Bill/PortalBundle/Entity/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)
{

}

Bill/PortalBundle/Entity/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)
{

}

最佳答案

如果你想使用连接表,你应该使用 ManyToMany 关系,而不是 ManyToOne :http://docs.doctrine-project.org/en/2.0.x/reference/association-mapping.html#many-to-many-bidirectional

对于第二个错误,很奇怪,因为您在构造方法中将 userroles 初始化为 ArrayCollection,它应该可以工作。您可以添加一个 var_dump 来查看此属性中存储的内容吗?

为什么不为用户角色获取任何 setter / getter ?

我认为您还应该阅读 Symfony 编码标准:http://symfony.com/doc/current/contributing/code/standards.html .您的编码风格不一致。

关于php - 创建用户角色 Symfony2 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20839712/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com