简体   繁体   English

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

[英]Issue in creating User Roles Symfony2

Simple process, Need to create Users with mapped Roles. 简单的过程,需要创建具有映射角色的用户。

I followed the step from link http://symfony.com/doc/current/cookbook/security/entity_provider.html 我遵循链接http://symfony.com/doc/current/cookbook/security/entity_provider.html的步骤

User and Roles table generated but users_roles table is not generated in MySql... Will i need to create it manually? 已生成用户和角色表,但未在MySql中生成users_roles表...我需要手动创建它吗?

Second I have configured with User table for Authentication 其次,我已经配置了用于身份验证的用户表

After login it redirects to Error page, 登录后,它将重定向到“错误”页面,

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

I searched, but i cant find any solutions... Below my code 我搜索了,但是找不到任何解决方案...代码下方

Bill/PortalBundle/Entity/users.php 比尔/ 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)
{

}

} }

Bill/PortalBundle/Entity/roles.php 比尔/ 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)
{

}   

} }

You should use a ManyToMany relation, not a ManyToOne if you want to use a join table : http://docs.doctrine-project.org/en/2.0.x/reference/association-mapping.html#many-to-many-bidirectional 如果要使用联接表,则应使用ManyToMany关系,而不要使用ManyToOne关系: http ://docs.doctrine-project.org/en/2.0.x/reference/association-mapping.html#many-to-many -bidirectional

For the second error, it's strange as you initialize usersroles as an ArrayCollection in your construct method, it should work. 对于第二个错误,当您在构造方法中将usersroles初始化为ArrayCollection时,这很奇怪,它应该可以工作。 Could you add a var_dump to look what is stored in this property ? 您可以添加var_dump来查看存储在此属性中的内容吗?

Why don't you get any setter/getter for usersroles ? 您为什么不为用户角色获得任何设置者/获取者?

I think you should also read Symfony coding standards : http://symfony.com/doc/current/contributing/code/standards.html . 我认为您还应该阅读Symfony编码标准: http : //symfony.com/doc/current/contributing/code/standards.html You coding style is not consistent. 您的编码风格不一致。

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

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