无法确定属性"角色的访问类型"

时间:2017-02-15 14:47:23

标签: php symfony formbuilder

我在尝试提交以下表单时收到以下错误消息 我认为错误必须来自User中角色的属性是userRoles而不是角色。我想知道是否有办法将EntityType字段映射到我查看此Answer的用户的属性,但它似乎不是解决此问题的正确方法,它更多一项工作。

class UserEditType extends AbstractType{

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('roles', EntityType::class, array('multiple'=> true, 'class' => 'AuthBundle:Role', 'choice_label' => 'slug','attr' => array('class'=>'form-control')))    
        ->add('email', EmailType::class, array('attr' => array('class'=>'form-control')))
        ->add('username', TextType::class, array('attr' => array('class'=>'form-control')))
        ->add('active', CheckboxType::class, array('attr' => array('class'=>'checkbox')));
}

public function configureOptions(OptionsResolver $resolver)
{
    $resolver->setDefaults(array(
        'data_class' => User::class,
    ));
}

}

用户类

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

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

/**
 * @Assert\Length(max=4096)
 * @Assert\Length(min=8)
 */
private $plainPassword;

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

/**
 * @ORM\Column(type="string", length=240, nullable=true)
 */
private $profilePicture;

/**
 * @ORM\Column(type="string", length=180, unique=true, options={"default" : "default.png"})
 */
private $email;

/**
 * @ORM\Column(name="is_active", type="boolean", options={"default" : 0})
 */
private $isActive;

/**
 * @ORM\ManyToMany(targetEntity="Role")
 * @ORM\JoinTable(name="user_role",
 *     joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")},
 *     inverseJoinColumns={@ORM\JoinColumn(name="role_id", referencedColumnName="id")}
 * )
 *
 * @var ArrayCollection $userRoles
 */
protected $userRoles;



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

}

public function getUsername()
{
    return $this->username;
}

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

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

public function getPassword()
{
    return $this->password;
}

public function getplainPassword()
{
    return $this->plainPassword;
}

public function getPicture()
{
    return $this->profilePicture;
}

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

public function eraseCredentials()
{
}

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

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

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

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

    return $this;
}

/**
 * Set password
 *
 * @param string $password
 *
 * @return User
 */
public function setPlainPassword($password)
{
    $this->plainPassword = $password;

    return $this;
}

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

    return $this;
}

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

    return $this;
}

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

/**
 * 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 boolean $isActive
 *
 * @return User
 */
public function setIsActive($isActive)
{
    $this->isActive = $isActive;

    return $this;
}

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

public function isAccountNonExpired()
{
    return true;
}

public function isAccountNonLocked()
{
    return true;
}

public function isCredentialsNonExpired()
{
    return true;
}

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

/**
 * Add userRole
 *
 * @param \AuthBundle\Entity\Role $userRole
 *
 * @return User
 */
public function addUserRole(\AuthBundle\Entity\Role $userRole)
{
    $this->userRoles[] = $userRole;

    return $this;
}

/**
 * Remove userRole
 *
 * @param \AuthBundle\Entity\Role $userRole
 */
public function removeUserRole(\AuthBundle\Entity\Role $userRole)
{
    $this->userRoles->removeElement($userRole);
}

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

角色等级

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

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

 /**
 * @ORM\Column(type="string", length=255)
 *
 * @var string $slug
 */
protected $slug;

/**
 * @ORM\Column(type="datetime", name="created_at")
 *
 * @var DateTime $createdAt
 */
protected $createdAt;

/**
* @ORM\ManyToMany(targetEntity="AuthBundle\Entity\User")
*/
protected $users;

/**
 * 
 */
public function __construct()
{
    $this->users = new ArrayCollection();
    $this->createdAt = new \DateTime();
}


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

/**
 * 
 *
 * @return string The name.
 */
public function getName()
{
    return $this->name;
}

/**
 * 
 *
 * @param string $value The name.
 */
public function setName($value)
{
    $this->name = $value;
}
/**
 * 
 *
 * @return string The name.
 */
public function getSlug()
{
    return $this->slug;
}

/**
 * 
 *
 * @param string $value The name.
 */
public function setSlug($value)
{
    $this->slug = $value;
}

/**
 * 
 *
 * @return DateTime A DateTime object.
 */
public function getCreatedAt()
{
    return $this->createdAt;
}

/**
 * RoleInterface.
 *
 * @return string The role.
 */
public function getRole()
{
    return $this->getName();
}

/**
 * Set createdAt
 *
 * @param \DateTime $createdAt
 *
 * @return Role
 */
public function setCreatedAt($createdAt)
{
    $this->createdAt = $createdAt;

    return $this;
}

/**
 * Add user
 *
 * @param \AuthBundle\Entity\User $user
 *
 * @return Role
 */
public function addUser(\AuthBundle\Entity\User $user)
{
    $this->users[] = $user;

    return $this;
}

/**
 * Remove user
 *
 * @param \AuthBundle\Entity\User $user
 */
public function removeUser(\AuthBundle\Entity\User $user)
{
    $this->users->removeElement($user);
}

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

0 个答案:

没有答案