改变实体关系后的意外行为 [英] unexpected behaviour following changing entity relationships

查看:148
本文介绍了改变实体关系后的意外行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



在一个早期线程中成功解决问题之后,我需要改变我的实体间关系现在在尝试将用户登录到应用程序时出现此错误:



CRITICAL - 未捕获PHP异常Symfony\Component\Debug\ Exception\UndefinedMethodException:尝试在Closure类上调用方法getRole。在C:\Dropbox\xampp\htdocs\etrack3\src\Ampisoft\Bundle\etrackBundle\Entity\Users.php行234



我从manyToMany关系更改为Users / Roles实体之间的manyToOne / OneToMany。



我读到serialize可能会导致问题,但我把它拿出来,没有任何区别。序列化所需的方法的残余仍然在那里,所以请忽略(除非是这个问题)。



请问有人告诉我我做错了什么?我想知道如果最好删除所有的数据库表并重新开始!!!!



这里是实体类。

  / ** 
*用户
*
* @ ORM\Table =users)
* @ ORM\Entity(repositoryClass =Ampisoft\Bundle\etrackBundle\Entity\UsersRepository)
* /
类用户实现UserInterface
{
/ **
* @var整数
*
* @ 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 $名字;

/ **
* @ ORM\Column(name =surname,type =string,length = 25)
* /
private $姓;

/ **
* @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 $角色;

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

/ **
*设置ID
*
* @return integer
* /
public function setId $ id)
{
$ this-> id = $ id;
return $ this;
}

/ **
*设置用户名
*
* @param string $ username
* @return用户
* /
public function setUsername($ username)
{
$ this-> username = $ username;

return $ this;
}

/ **
*获取用户名
*
* @return string
* /
public function getUsername )
{
return $ this-> username;
}

/ **
*设置密码
*
* @param string $ password
* @return用户
* /
public function setPassword($ password)
{
$ this-> password = $ password;

return $ this;
}

/ **
*获取密码
*
* @return string
* /
public function getPassword )
{
return $ this-> password;
}

/ **
*设置isActive
*
* @param boolean $ isActive
* @return用户
* /
public function setIsActive($ isActive)
{
$ this-> isActive = $ isActive;

return $ this;
}

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

/ **
*设置电子邮件
*
* @param string $ email
* @return用户
* /
public function setEmail($ email)
{
$ this-> email = $ email;

return $ this;
}

/ **
*获取电子邮件
*
* @return string
* /
public function getEmail )
{
return $ this-> email;
}

/ **
*设置lastLogged
*
* @param \DateTime $ lastLogged
* @return用户
* /
public function setLastLogged($ lastLogged)
{
$ this-> lastLogged = $ lastLogged;

return $ this;
}

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

/ **
*添加角色
*
* @param \Ampisoft\Bundle\etrackBundle\Entity\Roles $ roles
* @return users
* /
public function addRoles(Roles $ roles)
{
$ this-> roles [] = $ roles;

return $ this;
}

/ **
*删除角色
*
* @param \Ampisoft\Bundle\etrackBundle\Entity\Roles $ roles
* /
public function removeRoles(Roles $ roles)
{
$ this-> roles-> removeElement($ roles);
}

/ **
*设置名字
*
* @param string $ firstname
* @return用户
* /
public function setFirstname($ firstname)
{
$ this-> firstname = $ firstname;

return $ this;
}

/ **
*获取名字
*
* @return string
* /
public function getFirstname )
{
return $ this-> firstname;
}

/ **
*设置姓氏
*
* @param string $ lastname
* @return用户
* /
public function setLastname($ lastname)
{
$ this-> lastname = $ lastname;

return $ this;
}

/ **
*获取姓氏
*
* @return string
* /
public function getLastname )
{
return $ this-> lastname;
}


/ **
* @see \Serializable :: serialize()
* /
/ **
*序列化当前User对象的内容
* @return string
* /
public function serialize()
{
return \json_encode(
数组($ this-> username,$ this-> password,$ this-> salt,
$ this-> roles,$ this-> id));
}

/ **
*取消排序当前User对象中给定的字符串
* @param序列化
* /
public function unserialize($ serialized)
{
list($ this-> username,$ this-> password,$ this-> salt,
$ this-> roles,$ this - > id)= \json_decode(
$ serialized);
}

}

更新1
这已经工作,但是产生了一个新的错误,我会把它移动到一个新的问题,当定时器让我。



Catchable致命错误:参数4传递给Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken :: __ construct()必须是数组类型,给定对象,在C:\Dropbox中调用在线96上的\xampp\htdocs\etrack3\vendor\symfony\symfony\src\Symfony\Component\Security\Core\Authentication\Provider\UserAuthenticationProvider.php / code>

解决方案

HI我想你想要的不是这个

  public function getRoles()
{
$ roles = array();
foreach($ this-> roles as $ role){
$ roles [] = $ role-> getRole();
}

return $ roles;
}

但这个

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

角色应该是一个ArrayCollection,所以调用getRole(我假设)角色类似乎是可能的问题。



如果您使用的是Eclipse,那么类就是 Doctrine\Common\Collections \ArrayCollection; 这应该是您的角色集合,我也建议您正在使用IDE来执行此操作(类型提示)

  // ...在班级文件顶部减速之前
使用Doctrine\Common\Collections\ArrayCollection;

/ **
* @param ArrayCollection $ roles
* /
public function setRoles(ArrayCollection $ roles)
{
// ..类型转换输入只允许使用ArrayCollection类
$ this-> roles = $ roles;
}

/ **
* @return ArrayCollection
* /
public function getRoles()
{
return $这 - >角色;
}

还有一个很好的机会设置 $这个角色在某个时候成为一个标准数组。你应该总是输入你的输入到一个特定的类,如果它只能接受这种类型的类来排除错误,如使用一个纯数组。



最后的事情是通常保护的属性是首选的,因为后者可以扩展类,它在类之外不可访问非常像私有,但与私有不同,它可以在继承类中访问。


my nightmare of a day continues.....

after implimenting a successful solution in an earlier thread where I need to alter my inter-entity relationships I am now getting this error when I try to log a user into the app:

CRITICAL - Uncaught PHP Exception Symfony\Component\Debug\Exception\UndefinedMethodException: "Attempted to call method "getRole" on class "Closure"." at C:\Dropbox\xampp\htdocs\etrack3\src\Ampisoft\Bundle\etrackBundle\Entity\Users.php line 234

I changed from a manyToMany relationship, to a manyToOne/OneToMany between a Users/Roles entity.

Ive read that serialize could cause the issue but Ive taken it out and it didnt make any difference. The remnants of the serialize required methods are still in there so please ignore (unless they are the issue).

Please could someone tell me what Ive done wrong? Im wondering if its best to scrap all the database tables and start again!!!!

here is the entity class.

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

}

update 1 this has worked but produced a new error Im going to move it to a new question when the post timer lets me.

Catchable Fatal Error: Argument 4 passed to Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken::__construct() must be of the type array, object given, called in C:\Dropbox\xampp\htdocs\etrack3\vendor\symfony\symfony\src\Symfony\Component\Security\Core\Authentication\Provider\UserAuthenticationProvider.php on line 96 and defined

解决方案

HI I think what you want is not this

public function getRoles()
{
    $roles = array();
    foreach ($this->roles as $role) {
        $roles[] = $role->getRole();
    }

    return $roles;
}

but this

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

Roles should be an ArrayCollection already, so calling getRole on the ( I assume ) Role class seems to be possible the issue.

If you are using an IDE such as Eclipse then the class is Doctrine\Common\Collections\ArrayCollection; this should be your collection of Roles, I would suggest also is you are using an IDE to do something like this ( for type hinting )

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

Also there is a good chance you are setting $this->roles to be a standard array at some point. You should always type cast your input to a specific class if it can only accept that type of class to rule out errors such as using a plain array.

Last thing, is generally protected for the properties are preferred because latter you can extend the class, it's not accessible outside the class much like private, but unlike private it is accessible in inheriting classes.

这篇关于改变实体关系后的意外行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆