Php 更改实体关系后的意外行为
我一天的噩梦还在继续 在我需要更改实体间关系的早期线程中实施成功的解决方案后,我现在在尝试将用户登录到应用程序时遇到以下错误:Php 更改实体关系后的意外行为,php,symfony,doctrine-orm,Php,Symfony,Doctrine Orm,我一天的噩梦还在继续 在我需要更改实体间关系的早期线程中实施成功的解决方案后,我现在在尝试将用户登录到应用程序时遇到以下错误: CRITICAL-Uncaught PHP异常Symfony\Component\Debug\Exception\UndefinedMethodException:“试图调用类“Closure”上的方法“getRole”。“位于C:\Dropbox\xampp\htdocs\etrack3\src\Ampisoft\Bundle\etrackBundle\Entity\
CRITICAL-Uncaught PHP异常Symfony\Component\Debug\Exception\UndefinedMethodException:“试图调用类“Closure”上的方法“getRole”。“位于C:\Dropbox\xampp\htdocs\etrack3\src\Ampisoft\Bundle\etrackBundle\Entity\Users.PHP第234行
我将用户/角色实体之间的多人关系更改为多人/一人关系
我读到连载可能会引起问题,但我把它删掉了,没有任何区别。serialize required方法的剩余部分仍然存在,因此请忽略(除非它们是问题所在)
有人能告诉我我做错了什么吗?我想知道是不是最好放弃所有的数据库表,重新开始
这是实体类
/**
* user
*
* @ORM\Table(name="users")
* @ORM\Entity(repositoryClass="Ampisoft\Bundle\etrackBundle\Entity\UsersRepository")
*/
class Users implements UserInterface
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="username", type="string", length=25, unique=true)
*/
private $username;
/**
* @var string
*
* @ORM\Column(name="password", type="string", length=64)
*/
private $password;
/**
* @ORM\Column(name="firstname", type="string", length=25)
*/
private $firstname;
/**
* @ORM\Column(name="surname", type="string", length=25)
*/
private $lastname;
/**
* @var boolean
*
* @ORM\Column(name="isActive", type="boolean")
*/
private $isActive = 1;
/**
* @var string
*
* @ORM\Column(name="email", type="string", length=255, unique=true)
*/
private $email;
/**
* @var \DateTime
*
* @ORM\Column(name="lastLogged", type="string")
*/
private $lastLogged = '-0001-11-30 00:00:00';
/**
* @var string;
*
* @ORM\Column(name="salt", type="string", length=255)
*/
private $salt;
/**
* @ORM\ManyToOne(targetEntity="Roles", inversedBy="users")
*
*/
private $roles;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set Id
*
* @return integer
*/
public function setId($id)
{
$this->id = $id;
return $this;
}
/**
* 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;
}
/**
* 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;
}
/**
* 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 lastLogged
*
* @param \DateTime $lastLogged
* @return user
*/
public function setLastLogged($lastLogged)
{
$this->lastLogged = $lastLogged;
return $this;
}
/**
* Get lastLogged
*
* @return \DateTime
*/
public function getLastLogged()
{
return $this->lastLogged;
}
public function __construct()
{
$this->roles = new ArrayCollection();
$this->isActive = true;
}
/**
* @inheritDoc
*/
public function getRoles()
{
$roles = array();
foreach ($this->roles as $role) {
$roles[] = $role->getRole();
}
return $roles;
}
/**
* @param $roles
* @return $this
*/
public function setRoles($roles)
{
$this->roles = $roles;
return $this;
}
/**
* @inheritDoc
*/
public function eraseCredentials()
{
}
/**
* @inheritDoc
*/
public function getSalt()
{
return $this->salt;
}
public function setSalt($salt)
{
$this->salt = $salt;
return $this;
}
public function isAccountNonExpired()
{
return true;
}
public function isAccountNonLocked()
{
return true;
}
public function isCredentialsNonExpired()
{
return true;
}
public function isEnabled()
{
return $this->isActive;
}
/**
* Add roles
*
* @param \Ampisoft\Bundle\etrackBundle\Entity\Roles $roles
* @return users
*/
public function addRoles(Roles $roles)
{
$this->roles[] = $roles;
return $this;
}
/**
* Remove roles
*
* @param \Ampisoft\Bundle\etrackBundle\Entity\Roles $roles
*/
public function removeRoles(Roles $roles)
{
$this->roles->removeElement($roles);
}
/**
* Set firstname
*
* @param string $firstname
* @return users
*/
public function setFirstname($firstname)
{
$this->firstname = $firstname;
return $this;
}
/**
* Get firstname
*
* @return string
*/
public function getFirstname()
{
return $this->firstname;
}
/**
* Set lastname
*
* @param string $lastname
* @return users
*/
public function setLastname($lastname)
{
$this->lastname = $lastname;
return $this;
}
/**
* Get lastname
*
* @return string
*/
public function getLastname()
{
return $this->lastname;
}
/**
* @see \Serializable::serialize()
*/
/**
* Serializes the content of the current User object
* @return string
*/
public function serialize()
{
return \json_encode(
array($this->username, $this->password, $this->salt,
$this->roles, $this->id));
}
/**
* Unserializes the given string in the current User object
* @param serialized
*/
public function unserialize($serialized)
{
list($this->username, $this->password, $this->salt,
$this->roles, $this->id) = \json_decode(
$serialized);
}
}
更新1
这已经起作用了,但是产生了一个新的错误,我将在post timer允许的时候把它移到一个新的问题
Catchable致命错误:传递给Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken::\uu construct()的参数4必须是数组类型,给定对象,在第96行的C:\Dropbox\xampp\htdocs\etrack3\vendor\symfony\symfony\src\symfony\Component\Security\Core\Authentication\Provider\UserAuthenticationProvider.php中调用,并定义了
嗨,我想你想要的不是这个
public function getRoles()
{
$roles = array();
foreach ($this->roles as $role) {
$roles[] = $role->getRole();
}
return $roles;
}
但是这个
public function getRoles()
{
return $this->roles;
}
角色应该已经是ArrayCollection,因此在(我假设)Role类上调用getRole似乎是可能的问题
如果您使用的是Eclipse之类的IDE,那么类是Doctrine\Common\Collections\ArrayCollection代码>这应该是您的角色集合,我还建议您使用IDE来做类似的事情(用于类型提示)
另外,您很有可能在某个时候将$this->roles
设置为标准数组。如果特定类只能接受该类型的类以排除错误(例如使用普通数组),则应始终将输入类型强制转换为该类
最后一件事,通常是受保护的,因为属性是首选的,因为后者可以扩展类,它在类外不可访问,就像private一样,但与private不同,它在继承类中可访问。Closure是lambada风格函数的类(内置于PHP中),例如$f=function(){},我不确定为什么您要在您的ArrayCollecdtion中访问它,doctrine使用它来处理这些事情,它说的是什么$rolls is\doctrine\Common\Util\Debug::dump($this->roles);班上所有的东西都是食谱上的,所以我也不确定!我真的快没脑子了。。我希望雾很快就会过去。非常感谢。它澄清了这个问题,但产生了进一步的错误。我将更新问题。我使用PHPstorm顺便说一句:)
//... at the top of the class file before the class deceleration
use Doctrine\Common\Collections\ArrayCollection;
/**
* @param ArrayCollection $roles
*/
public function setRoles(ArrayCollection $roles)
{
//.. type cast the input to allow only use of ArrayCollection class
$this->roles = $roles;
}
/**
* @return ArrayCollection
*/
public function getRoles()
{
return $this->roles;
}