关系多对多的双向Symfony 3

时间:2017-06-09 18:37:11

标签: php symfony doctrine-orm

我正在使用Symfony 3和Doctrine。我的应用程序中有2个实体,用户和角色。我需要建立多对多的双向关系。 我查阅了学说文档。这是代码:

class Rol
{
/**
 * @var int
 *
 * @ORM\Column(name="id", type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 */
private $id;

/**
 * @var string
 *
 * @ORM\Column(name="nombre", type="string", length=255, unique=true)
 */
private $nombre;

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

/**
 * @ORM\ManyToMany(targetEntity="Funcionalidad", inversedBy="rolesUsuario")
 * @ORM\JoinTable(name="rol_funcionalidad")
 */
private $funcionalidades;

/**
 * @ORM\ManyToMany(targetEntity="Usuario", mappedBy="rolesUsuario")
 */
private $usuarios;


public function getAttributes()
{
    return get_class_vars(__CLASS__);
}

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

/**
 * Set nombre
 *
 * @param string $nombre
 *
 * @return Rol
 */
public function setNombre($nombre)
{
    $this->nombre = $nombre;

    return $this;
}

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

/**
 * Set descripcion
 *
 * @param string $descripcion
 *
 * @return Rol
 */
public function setDescripcion($descripcion)
{
    $this->descripcion = $descripcion;

    return $this;
}

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

/**
 * @return string
 */
public function __toString()
{
    return $this->getNombre();
}
/**
 * Constructor
 */
public function __construct()
{
    $this->funcionalidades = new \Doctrine\Common\Collections\ArrayCollection();
    $this->usuarios = new \Doctrine\Common\Collections\ArrayCollection();
}

/**
 * Add funcionalidade
 *
 * @param \CECMED\SeguridadBundle\Entity\Rol $funcionalidade
 *
 * @return Rol
 */
public function addFuncionalidade(\CECMED\SeguridadBundle\Entity\Rol $funcionalidade)
{
    $this->funcionalidades[] = $funcionalidade;

    return $this;
}

/**
 * Remove funcionalidade
 *
 * @param \CECMED\SeguridadBundle\Entity\Rol $funcionalidade
 */
public function removeFuncionalidade(\CECMED\SeguridadBundle\Entity\Rol $funcionalidade)
{
    $this->funcionalidades->removeElement($funcionalidade);
}

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

/**
 * Add usuario
 *
 * @param \CECMED\SeguridadBundle\Entity\Usuario $usuario
 *
 * @return Rol
 */
public function addUsuario(\CECMED\SeguridadBundle\Entity\Usuario $usuario)
{
    $usuario->addRolesUsuario($this);
    $this->usuarios[] = $usuario;

    return $this;
}

/**
 * Remove usuario
 *
 * @param \CECMED\SeguridadBundle\Entity\Usuario $usuario
 */
public function removeUsuario(\CECMED\SeguridadBundle\Entity\Usuario $usuario)
{
    $this->usuarios->removeElement($usuario);
}

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

这是Usuario Class

  class Usuario implements AdvancedUserInterface
  {
  /**
  * @var int
  *
  * @ORM\Column(name="id", type="integer")
  * @ORM\Id
  * @ORM\GeneratedValue(strategy="AUTO")
  */
  private $id;

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

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

/**
 * @var string
 *
 * @ORM\Column(name="estado", type="boolean")
 */
private $estado;

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

/**
 * @ORM\ManyToMany(targetEntity="Rol", inversedBy="usuarios")
 * @ORM\JoinTable(name="usuario_rol")
 */
private $rolesUsuario;

/**
 * @ORM\OneToOne(targetEntity="Persona")
 * @ORM\JoinColumn(name="persona_id", referencedColumnName="id")
 */
private $persona;

public function getAttributes()
{
    return get_class_vars(__CLASS__);
}

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

/**
 * Set username
 *
 * @param string $username
 *
 * @return Usuario
 */
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 Usuario
 */
public function setPassword($password)
{
    $this->password = $password;

    return $this;
}

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

/**
 * Set estado
 *
 * @param string $estado
 *
 * @return Usuario
 */
public function setEstado($estado)
{
    $this->estado = $estado;

    return $this;
}

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

/**
 * Checks whether the user's account has expired.
 *
 * Internally, if this method returns false, the authentication system
 * will throw an AccountExpiredException and prevent login.
 *
 * @return bool true if the user's account is non expired, false otherwise
 *
 * @see AccountExpiredException
 */
public function isAccountNonExpired()
{
    return true;
}

/**
 * Checks whether the user is locked.
 *
 * Internally, if this method returns false, the authentication system
 * will throw a LockedException and prevent login.
 *
 * @return bool true if the user is not locked, false otherwise
 *
 * @see LockedException
 */
public function isAccountNonLocked()
{
    return true;
}

/**
 * Checks whether the user's credentials (password) has expired.
 *
 * Internally, if this method returns false, the authentication system
 * will throw a CredentialsExpiredException and prevent login.
 *
 * @return bool true if the user's credentials are non expired, false otherwise
 *
 * @see CredentialsExpiredException
 */
public function isCredentialsNonExpired()
{
    return true;
}

/**
 * Checks whether the user is enabled.
 *
 * Internally, if this method returns false, the authentication system
 * will throw a DisabledException and prevent login.
 *
 * @return bool true if the user is enabled, false otherwise
 *
 * @see DisabledException
 */
public function isEnabled()
{
    if($this->estado == true){
        return true;
    }else{
        return false;
    }
}

/**
 * Returns the roles granted to the user.
 *
 * <code>
 * public function getRoles()
 * {
 *     return array('ROLE_USER');
 * }
 * </code>
 *
 * Alternatively, the roles might be stored on a ``roles`` property,
 * and populated in any number of different ways when the user object
 * is created.
 *
 * @return (Role|string)[] The user roles
 */

public function getRolesUsuario()
{
    return $this->rolesUsuario;
}


public function getRoles()
{
    $r = array();
    foreach ($this->rolesUsuario as $roles){
        $r[] = $roles->getNombre();
    }
    return $r;
}

/**
 * Returns the salt that was originally used to encode the password.
 *
 * This can return null if the password was not encoded using a salt.
 *
 * @return string|null The salt
 */
public function getSalt()
{
    return $this->salt;
}

/**
 * Removes sensitive data from the user.
 *
 * This is important if, at any given point, sensitive information like
 * the plain-text password is stored on this object.
 */
public function eraseCredentials()
{
    return false;
}
/**
 * Constructor
 */
public function __construct()
{
    $this->rolesUsuario = new \Doctrine\Common\Collections\ArrayCollection();
}

/**
 * Set salt
 *
 * @param string $salt
 *
 * @return Usuario
 */
public function setSalt($salt)
{
    $this->salt = $salt;

    return $this;
}

/**
 * Add role
 *
 * @param \CECMED\SeguridadBundle\Entity\Rol $role
 *
 * @return Usuario
 */
public function addRole(\CECMED\SeguridadBundle\Entity\Rol $role)
{
    $this->rolesUsuario[] = $role;

    return $this;
}

/**
 * Remove role
 *
 * @param \CECMED\SeguridadBundle\Entity\Rol $role
 */
public function removeRole(\CECMED\SeguridadBundle\Entity\Rol $role)
{
    $this->rolesUsuario->removeElement($role);
}

/**
 * Set persona
 *
 * @param \CECMED\SeguridadBundle\Entity\persona $persona
 *
 * @return Usuario
 */
public function setPersona(\CECMED\SeguridadBundle\Entity\persona $persona = null)
{
    $this->persona = $persona;

    return $this;
}

/**
 * Get persona
 *
 * @return \CECMED\SeguridadBundle\Entity\persona
 */
public function getPersona()
{
    return $this->persona;
}

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

/**
 * Add rolesUsuario
 *
 * @param \CECMED\SeguridadBundle\Entity\Rol $rolesUsuario
 *
 * @return Usuario
 */
public function addRolesUsuario(\CECMED\SeguridadBundle\Entity\Rol $rolesUsuario)
{
    $this->rolesUsuario[] = $rolesUsuario;

    return $this;
}

/**
 * Remove rolesUsuario
 *
 * @param \CECMED\SeguridadBundle\Entity\Rol $rolesUsuario
 */
public function removeRolesUsuario(\CECMED\SeguridadBundle\Entity\Rol $rolesUsuario)
{
    $this->rolesUsuario->removeElement($rolesUsuario);
}
}

问题在于,当我使用cli和CRUD生成模式时,Usuario表单工作正常并且它在数据库中注册了角色,但是当我插入记录时Rol表单没有在数据库中注册用户{{3 }}

1 个答案:

答案 0 :(得分:0)

我在Symfony中找到的外键是@ORM \ JoinColumn是多余的。 Doctrine可以自己找到@ORM \ ManyToMany中的列。我认为在文档中并不清楚。我没有尝试过多对多的关系,但是我已经做了很多很多人。在我删除所有JoinColumns之前,我遇到了几个问题。试试看。让我知道事情的后续。