簡體   English   中英

關系多對多雙向Symfony 3

[英]Relation Many to Many Bidirectional Symfony 3

我正在將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;
}
}

這是烏蘇里奧課

  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表單未在數據庫中注冊用戶 在此處輸入圖片說明

我在Symfony中用外鍵發現的是@ORM \\ JoinColumn是多余的。 教義可以從@ORM \\ ManyToMany單獨找到該列。 我認為文檔尚不清楚。 我沒有在“多對多”關系中嘗試過這種方法,但是我做了很多“多對多”關系。 在刪除所有JoinColumns之前,我遇到了幾個問題。 試試看。 讓我知道事情的后續。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM