我不知道发生了什么

时间:2019-04-17 14:49:28

标签: symfony symfony4

我得到: 错误:App \ Entity \ User类包含1个抽象方法,因此必须声明为abstract或实现其余方法

  

(Symfony \ Component \ Security \ Core \ User \ UserInterface :: getUsername)

use Symfony\Component\Security\Core\User\UserInterface; 
    use App\Entity\Role; 
    /**  
    * @ORM\Entity(repositoryClass="App\Repository\UserRepository")  
    */ 
    class User implements UserInterface, \Serializable {     
    /**      
    * @ORM\Id()      
    * @ORM\GeneratedValue()      
    * @ORM\Column(type="integer")..

我需要创建一个注册表。

1 个答案:

答案 0 :(得分:0)

您正在实现UserInterface,因此必须实现所有关联的方法。 在您的情况下,getUsername出错,因此将其添加到您的实体中:

public function getUsername(): ?string
{
    return $this->username;
}
public function setUsername(string $username): void
{
    $this->username = $username;
}

您可能还有其他实现(see demo)的方法,例如:

public function setFullName(string $fullName): void
{
    $this->fullName = $fullName;
}
public function getFullName(): ?string
{
    return $this->fullName;
}
public function getEmail(): ?string
{
    return $this->email;
}
public function setEmail(string $email): void
{
    $this->email = $email;
}
public function getPassword(): ?string
{
    return $this->password;
}
public function setPassword(string $password): void
{
    $this->password = $password;
}
/**
 * Returns the roles or permissions granted to the user for security.
 */
public function getRoles(): array
{
    $roles = $this->roles;
    // guarantees that a user always has at least one role for security
    if (empty($roles)) {
        $roles[] = 'ROLE_USER';
    }
    return array_unique($roles);
}
public function setRoles(array $roles): void
{
    $this->roles = $roles;
}
/**
 * Returns the salt that was originally used to encode the password.
 *
 * {@inheritdoc}
 */
public function getSalt(): ?string
{
    // See "Do you need to use a Salt?" at https://symfony.com/doc/current/cookbook/security/entity_provider.html
    // we're using bcrypt in security.yml to encode the password, so
    // the salt value is built-in and you don't have to generate one
    return null;
}
/**
 * Removes sensitive data from the user.
 *
 * {@inheritdoc}
 */
public function eraseCredentials(): void
{
    // if you had a plainPassword property, you'd nullify it here
    // $this->plainPassword = null;
}
/**
 * {@inheritdoc}
 */
public function serialize(): string
{
    // add $this->salt too if you don't use Bcrypt or Argon2i
    return serialize([$this->id, $this->username, $this->password]);
}
/**
 * {@inheritdoc}
 */
public function unserialize($serialized): void
{
    // add $this->salt too if you don't use Bcrypt or Argon2i
    [$this->id, $this->username, $this->password] = unserialize($serialized, ['allowed_classes' => false]);
}