Symfony:带有学说的用户角色:没有创建任何实体或表

时间:2014-01-31 09:05:29

标签: symfony doctrine

我按照Symfony-Book中给出的说明获取用户角色关系,并在数据库中更新它们。 Symfony - User/Role - Doctrine

但是当我通过doctrine:generate:entities更新我的实体时,它只会生成现有的User.php,但不会触及Role.php。如果我的数据库中的表格会以doctrine:schema:update --force更新,这不会打扰我。但他们没有。提到的表格未创建。事实上,没有任何反应。你能帮我么?我期待着任何好的解决方案。

我的User.php

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

/**
* User
*
* @ORM\Table(name="user")
* @ORM\Entity
* @ORM\ManyToMany(targetEntity="Role", inversedBy="users")
*/
class User implements AdvancedUserInterface, \Serializable
{
/**
 * @var string
 *
 * @ORM\Column(name="username", type="string", length=255, nullable=false)
 */
private $username;

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

/**
 * @var string
 *
 * @ORM\Column(name="email", type="string", length=24, nullable=false)
 */
private $email;

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

/**
 * @var string
 *
 * @ORM\Column(name="type", type="string", length=10, nullable=false)
 */

/**
 * @var integer
 *
 * @ORM\Column(name="id", type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="IDENTITY")
 */
private $id;
private $roles;


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

public function getSalt()
{
    // you *may* need a real salt depending on your encoder
    // see section on salt below
    return null;
}

/**
 * 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 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;
}

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

/**
 * 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 isactive
 *
 * @param string $isactive
 * @return User
 */
public function setIsactive($isactive)
{
    $this->isactive = $isactive;

    return $this;
}

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


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

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);
}


/**
 * @var string
 */
private $type;


/**
 * Set type
 *
 * @param string $type
 * @return User
 */
public function setType($type)
{
    $this->type = $type;

    return $this;
}

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

public function isAccountNonExpired()
{
    return true;
}

public function isAccountNonLocked()
{
    return true;
}

public function isCredentialsNonExpired()
{
    return true;
}

public function isEnabled()
{
    return $this->isActive;
}
}

Role.php:

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

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

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

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

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

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

/**
 * @see RoleInterface
 */
public function getRole()
{
    return $this->role;
}
}

1 个答案:

答案 0 :(得分:0)

第一个提示:给你的用户表一个不同的名字,例如@ORM \ Table(name =“acme_user”)。

第二:添加用户类

/**
 * User's roles.
 * 
 * @var ArrayCollection
 * 
 * @ORM\ManyToMany(targetEntity="Role", inversedBy="users")
 */
private $roles;

并在角色类

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

@see:http://symfony.com/doc/2.3/cookbook/security/entity_provider.html