Symfony 不在 User<->Roles 表中保存 m:n 关系

Symfony doesnt save m:n relationship at User<->Roles tables

symfony2 不保存关系 table (role_user) 中的关系。 我想要从用户到角色的 m:n 关系。

实体中是否缺少组件?

谢谢!

角色实体

<?php
/**
 * Created by PhpStorm.
 * User: christianschade
 * Date: 02.02.15
 * Time: 19:29
 */

namespace Chris\UserBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Security\Core\Role\RoleInterface;
/**
 * Role
 * @ORM\Entity(repositoryClass="Chris\UserBundle\Entity\RoleRepository")
 * @ORM\Table(name="roles")
 */

class Role implements RoleInterface{
    /**
     * @ORM\Column(type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;
    public function getRole() {

        return $this->role;
    }
    /**
     * @ORM\Column(name="roles", type="json_array")
     */
    private $roles =  array();
    /**
     * @ORM\ManyToMany(targetEntity = "User", inversedBy = "roles")
     *
     * @var ArrayCollection $users
     */
    protected $users;

    /**
     * @return ArrayCollection
     */
    public function getUsers()
    {
        return $this->users;
    }

    /**
     * @param ArrayCollection $users
     */
    public function setUsers($users)
    {
        $this->users = $users;
    }


    public function setRoles(array $roles)
    {
        $this->roles = $roles;

        // allows for chaining
        return $this;
    }
    /**
     * @inheritDoc
     */
    public function getId()
    {
        return $this->id;
    }
}

用户实体

<?php

namespace Chris\UserBundle\Entity;

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

/**
 * User
 *
 * @ORM\Table(name="users")
 * @ORM\Entity(repositoryClass="Chris\UserBundle\Entity\UserRepository")
 */
class User implements  UserInterface,\Serializable
{
    /**
     * @ORM\ManyToMany(targetEntity = "Role", mappedBy = "users")
     *
     * @var ArrayCollection $roles;
     */
    private $roles;

    /**
     * @inheritDoc
     */
    public function getRoles()
    {
        $roles2 = array();
        foreach ($this->roles as $role) {
            $roles2[] = $role->getRole();
        }

        return $roles2;
    }

    public function setRoles($roles){
        $this->roles[] = $roles;
    }

    /**
     * @ORM\Column(type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

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

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

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

    /**
     * @ORM\Column(type="string", length=60)
     */
    private $vorname;

    /**
     * @ORM\Column(type="string", length=60)
     */
    private $nachname;

    /**
     * @return mixed
     */
    public function getVorname()
    {
        return $this->vorname;
    }

    /**
     * @param mixed $vorname
     */
    public function setVorname($vorname)
    {
        $this->vorname = $vorname;
    }

    /**
     * @return mixed
     */
    public function getNachname()
    {
        return $this->nachname;
    }

    /**
     * @param mixed $nachname
     */
    public function setNachname($nachname)
    {
        $this->nachname = $nachname;
    }

    /**
     * @return mixed
     */
    public function getLetzterLogin()
    {
        return $this->letzterLogin;
    }

    /**
     * @param mixed $letzterLogin
     */
    public function setLetzterLogin($letzterLogin)
    {
        $this->letzterLogin = $letzterLogin;
    }

    /**
     * @return mixed
     */
    public function getIsActive()
    {
        return $this->isActive;
    }

    /**
     * @param mixed $isActive
     */
    public function setIsActive($isActive)
    {
        $this->isActive = $isActive;
    }

    /**
     * @ORM\Column(type="datetime", nullable=true)
     */
    private $letzterLogin;



    /**
     * @ORM\Column(name="is_active", type="boolean")
     */
    private $isActive;




    public function __construct()
    {
        $this->isActive = true;
        // may not be needed, see section on salt below
        // $this->salt = md5(uniqid(null, true));
    }

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

    /**
     * @inheritDoc
     */
    public function getUsername()
    {
        return $this->username;
    }

    public function setUsername($username)
    {
        $this->username = $username;
    }
    /**
     * @inheritDoc
     */
    public function getSalt()
    {
        // you *may* need a real salt depending on your encoder
        // see section on salt below
        return null;
    }

    /**
     * @inheritDoc
     */
    public function getPassword()
    {
        return $this->password;
    }

    public function setPassword($password)
    {
        $this->password = $password;
    }

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }


    /**
     * @inheritDoc
     */
    public function eraseCredentials()
    {
    }

    /**
     * @see \Serializable::serialize()
     */
    public function serialize()
    {
        return serialize(array(
            $this->id,
            $this->username,
            $this->password,
            // see section on salt below
            // $this->salt,
        ));
    }

    /**
     * @see \Serializable::unserialize()
     */
    public function unserialize($serialized)
    {
        list (
            $this->id,
            $this->username,
            $this->password,
            // see section on salt below
            // $this->salt
            ) = unserialize($serialized);
    }
}

用户加载夹具

public function load(ObjectManager $manager)
    {

            $user = new User();
            $user->setUsername($i);
            $user->setIsActive(1);
            $user->setLetzterLogin(new \DateTime("2015-02-01 14:00"));
            $user->setPassword($this->encodePassword($user,"user"));
            $user->setEmail($i . "@" .$i .".de");
            $user->setVorname("Christian");
            $user->setNachname("Schade");
            $role = $manager->getRepository('UserBundle:Role')->findRoleByName("USER");
            $user->setRoles($role);
            $manager->persist($user);



        $manager->flush();



    }

需要在站长网站设置JoinTable,看一下ORM documentation:
http://doctrine-orm.readthedocs.org/en/latest/reference/association-mapping.html

6.86.9 部分,您将看到它是如何完成的

你需要在用户实体luke中写关系:

/**
 * @ORM\ManyToMany(targetEntity="Role", inversedBy="users")
 * @ORM\JoinTable(name="role_user",
 *      joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")},
 *      inverseJoinColumns={@ORM\JoinColumn(name="role_id", referencedColumnName="id")}
 *      )
 */
private $roles;