doubipiao1611 2015-08-11 18:33
浏览 7

使用ManyToOne登录

I'm a beginner on Symfony. I'm trying to make a login with user roles.

I tried to make it with ManyToMany and then on getRole, it returned me an object. So I could get a name of user roles.

$res = array();

foreach ($this->roles as $value) {
    $res[] = $value->getRole();
}

But now after switching from ManyToMany to OneToMany (role.php) & ManyToOne (user.php). It stopped working. getRole() returns a number, not the name of rule.

Where do I make a mistake? The reason why I can't do it is the 2 tables don't connect to each other, right?

Could someone help me with it, please? Thx in advance.

User.php

 namespace AppBundle\Entity;

 use Doctrine\ORM\Mapping as ORM;
 use Symfony\Component\Security\Core\User\UserInterface;
 use Doctrine\Common\Collections\ArrayCollection;

 /**
  * @ORM\Entity(repositoryClass="AppBundle\Repository\UserRepository")
  *
  * @ORM\Table(name="User")
  */
class User implements UserInterface
{
/**
 * @ORM\Id
 * @ORM\GeneratedValue
 * @ORM\Column(name="use_id", type="integer")
 */
private $id;

/**
 * @ORM\Column(name="use_name", type="string", unique=true)
 */
private $username;

/**
 * @ORM\Column(name="use_email", type="string", unique=true)
 */
private $email;

/**
 * @ORM\Column(name="use_pwd", type="string")
 */
private $password;

/**
 * @ORM\Column(name="use_role", type="integer")
 * @ORM\ManyToOne(targetEntity="Role", inversedBy="users")
 * @ORM\JoinColumn(name="rol_id", referencedColumnName="id")
 */
private $roles;

/**
 * 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 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 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 roles
 *
 * @param integer $roles
 *
 * @return User
 */
public function setRoles($roles)
{
    $this->roles = $roles;

    return $this;
}

/**
 * Get roles
 *
 * @return string
 */
public function getRoles()
{
    return $this->roles;
}

public function eraseCredentials() {

}

public function getSalt() {
    return null;
}

public function serialize() {
    return serialize(array(
        $this->id,
        $this->email,
        $this->password
    ));
}

public function unserialize($serialized) {
    list (
        $this->id,
        $this->email,
        $this->password
        ) = unserialize($serialized);
}
}

Role.php

<?php

 namespace AppBundle\Entity;

 use Doctrine\Common\Collections\ArrayCollection;
 use Doctrine\Common\Collections\Collection;
 use Doctrine\ORM\Mapping as ORM;
 use Symfony\Component\Security\Core\Role\RoleInterface;

 /**
  * Role
  *
  * @ORM\Table(name="Role")
  * @ORM\Entity
  */
 class Role implements RoleInterface
 {
/**
 * @var integer
 *
 * @ORM\Column(name="rol_id", type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 */
private $id;

/**
 * @var string
 *
 * @ORM\Column(name="role_name", type="string", length=40)
 */
private $name;

/**
 * @ORM\OneToMany(targetEntity="User", mappedBy="roles")
 */
private $users;

public function __construct() {
    $this->users = new ArrayCollection();
}

/**
 * Get id
 *
 * @return integer
 */
public function getId()
{
    return $this->id;
}

/**
 * Set name
 *
 * @param string $name
 *
 * @return Role
 */
public function setName($name)
{
    $this->name = $name;

    return $this;
}

/**
 * Get name
 *
 * @return string
 */
public function getName()
{
    return $this->name;
}

/**
 * Add user
 *
 * @param \AppBundle\Entity\User $user
 *
 * @return Role
 */
public function addUser(User $user)
{
    $this->users[] = $user;

    return $this;
}

/**
 * Remove user
 *
 * @param \AppBundle\Entity\User $user
 */
public function removeUser(User $user)
{
    $this->users->removeElement($user);
}

/**
 * Get users
 *
 * @return \Doctrine\Common\Collections\Collection
 */
public function getUsers()
{
    return $this->users;

}

/**
 * Returns the role.
 *
 * This method returns a string representation whenever possible.
 *
 * When the role cannot be represented with sufficient precision by a
 * string, it should return null.
 *
 * @return string|null A string representation of the role, or null
 */
public function getRole()
{
    return $this->name;
    // TODO: Implement getRole() method.
}
}
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
    • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
    • ¥15 手机接入宽带网线,如何释放宽带全部速度
    • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
    • ¥15 ETLCloud 处理json多层级问题
    • ¥15 matlab中使用gurobi时报错
    • ¥15 这个主板怎么能扩出一两个sata口
    • ¥15 不是,这到底错哪儿了😭
    • ¥15 2020长安杯与连接网探
    • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么