T
T
Tema2017-11-13 11:14:13
symfony
Tema, 2017-11-13 11:14:13

Why does User (sf3) always return only one role?

I understand with sf3. The problem is that during (custom) authentication, the user selects only one role, the first one found in the database. I look at the profiler - the request for obtaining roles from the database is not limited by the number of lines.
User entity:

/**
 * @ORM\Entity
 * @ORM\Table(name="users")
 */
class User implements UserInterface, \Serializable
{
    // ...

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

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

    public function getRoles()
    {
        return $this->roles->toArray();
    }

    // ...
}

Role entity:
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Security\Core\Role\Role as BaseRole;

/**
 * @ORM\Entity
 * @ORM\Table(name="users_role")
 */
class Role extends BaseRole
{
    /**
     * @ORM\Id
     * @ORM\ManyToOne(targetEntity="User", inversedBy="roles")
     */
    private $user;

    /**
     * @ORM\Column(type="string", length=30)
     */
    protected $role;

    // ...
}

What's wrong with mapping?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander, 2017-11-13
@Tem_ka

Offhand, I can assume that the problem is in the Role entity, you have the Id annotation in $user. This means that you must have a unique user_id for each entry. So I can’t even imagine how you managed to assign 2 roles for 1 user, if this database should not roll around for you.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question