繁体   English   中英

创建用户角色Symfony2时出现问题

[英]Issue in creating User Roles Symfony2

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

我遵循链接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)
{

}   

}

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

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

您为什么不为用户角色获得任何设置者/获取者?

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM