Symfony2 Inserting Error “Warning: spl_object_hash

2019-09-08 14:57发布

when I try to persist my object and flush it I get this error message:

Warning: spl_object_hash() expects parameter 1 to be object, integer given 500 Internal Server Error - ContextErrorException

I know this kind of question has been posted a lot in stack overflow but it still couldn't solve my problem. That's why I ask again here, hopefully some one can help me.

Below are my code for to persist the user class:

$package = $em->getRepository('MyBundle:Package')->findOneBy(array('id' => 1));
$new_user->addPackage($package);

$role = $em->getRepository('MyBundle:Role')->findOneBy(array('id' => 3));
$new_user->addRole($role);

$new_user->setCmsPrize(2); //int
$new_user->setCmsBet(3); //int
$new_user->setName("new user");
$new_user->setUserName("test123");
$new_user->setPassword("abc");
$new_user->setCreditLimit(1000);
$new_user->setCreditBalance(2000);
$new_user->setSelectedPackage($currentuser->getSelectedPackage());
$new_user->setParentId($currentuser->getId());
$new_user->setLayer($currentuser->getLayer() + 1);
$em->persist($new_user);
$em->flush();

Below is my user class:

/**
 * MyBundle\Entity\User
 *
 * @ORM\Table(name="")
 * @ORM\Entity(repositoryClass="MyBundle\Entity\UserRepository")
 */
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;

    /**
     * @ORM\Column(type="string", length=64, options={"fixed" = true}))
     */
    private $password;

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

    /**
     * @ORM\OneToMany(targetEntity="User", mappedBy="parent_id")
     * */
    private $children;

    /**
     * @ORM\ManyToOne(targetEntity="User", inversedBy="children")
     * @ORM\JoinColumn(name="parent_id", referencedColumnName="id", nullable=true)
     * 
     */
    private $parent_id;

    /**
     * @ORM\Column(name="layer", type="integer")
     */
    private $layer;

    /**
     * @ORM\Column(name="credit_limit", type="float")
     */
    private $credit_limit;

    /**
     * @ORM\Column(name="credit_balance", type="float")
     */
    private $credit_balance;

    /**
     * @ORM\Column(name="cms_bet", type="integer")
     */
    private $cms_bet;

    /**
     * @ORM\Column(name="cms_prize", type="integer")
     */
    private $cms_prize;

    /**
     * @ORM\OneToOne(targetEntity="OneToOnePackage", inversedBy="users")
     * @ORM\JoinColumn(name="selected_package", referencedColumnName="id")
     */
    private $selected_package;

    /**
     * @ORM\Column(name="is_allow_open_acc", type="boolean")
     */
    private $is_allow_open_acc;

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

    /**
     * @ORM\Column(name="created_at", type="datetime")
     */
    private $created_at;

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

    /**
     * @ORM\ManyToMany(targetEntity="Package", inversedBy="users")
     *
     */
    private $packages;

    public function __construct() {
        //$this->isActive = true;
        $this->roles = new ArrayCollection();
        $this->packages = new ArrayCollection();
        $this->created_at = new \DateTime('NOW');//date('Y-m-d H:i:s');
        $this->credit_balance = 0;
        $this->credit_limit = 0;
        $this->status = 'ACTIVE';
        $this->is_allow_open_acc = true;
        $this->children = new ArrayCollection();
        // may not be needed, see section on salt below
        // $this->salt = md5(uniqid(null, true));
    }

    /**
     * @inheritDoc
     */
    public function getUsername() {
        return $this->username;
    }

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

    /**
     * @inheritDoc
     */
    public function getPassword() {
        return $this->password;
    }

    /**
     * @inheritDoc
     */
    public function getRoles() {
        return $this->roles->toArray();
    }

    /**
     * @inheritDoc
     */
    public function eraseCredentials() {

    }

    /**
     * @see \Serializable::serialize()
     */
    public function serialize() {
        return serialize(array(
            $this->id,
            $this->username,
            $this->password,
            $this->name,
            $this->is_allow_open_acc,
            $this->created_at,
            $this->credit_balance,
            $this->credit_limit,
            $this->parent_id,
            $this->status,
            $this->cms_bet,
            $this->cms_prize,
                // see section on salt below
                // $this->salt,
        ));
    }

    /**
     * @see \Serializable::unserialize()
     */
    public function unserialize($serialized) {
        list (
                $this->id,
                $this->username,
                $this->password,
                $this->name,
                $this->is_allow_open_acc,
                $this->created_at,
                $this->credit_balance,
                $this->credit_limit,
                $this->parent_id,
                $this->status,
                $this->cms_bet,
                $this->cms_prize,
                // see section on salt below
                // $this->salt
                ) = unserialize($serialized);
    }

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

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

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

    /**
     * Get isActive
     *
     * @return boolean 
     */
    public function getIsActive() {
        if ($this->status == 'ACTIVE') {
            return true;
        } else {
            return false;
        }
    }

    public function isAccountNonExpired() {
        return true;
    }

    public function isAccountNonLocked() {
        return true;
    }

    public function isCredentialsNonExpired() {
        return true;
    }

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

    /**
     * Add roles
     *
     * @param \MyBundle\Entity\Role $roles
     */
    public function addRole(\MyBundle\Entity\Role $roles) {
        $this->roles[] = $roles;
    }

    /**
     * Remove roles
     *
     * @param \MyBundle\Entity\Role $roles
     */
    public function removeRole(\MyBundle\Entity\Role $roles) {
        $this->roles->removeElement($roles);
    }

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

        return $this;
    }

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

    /**
     * Set parent_id
     *
     * @param integer $parentId
     */
    public function setParentId($parentId) {
        $this->parent_id = $parentId;
    }

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

    /**
     * Set credit_limit
     *
     * @param float $creditLimit
     */
    public function setCreditLimit($creditLimit) {
        $this->credit_limit = $creditLimit;
    }

    /**
     * Get credit_limit
     *
     * @return float 
     */
    public function getCreditLimit() {
        return $this->credit_limit;
    }

    /**
     * Set credit_balance
     *
     * @param float $creditBalance
     */
    public function setCreditBalance($creditBalance) {
        $this->credit_balance = $creditBalance;
    }

    /**
     * Get credit_balance
     *
     * @return float 
     */
    public function getCreditBalance() {
        return $this->credit_balance;
    }

    /**
     * Set is_allow_open_acc
     *
     * @param boolean $isAllowOpenAcc
     */
    public function setIsAllowOpenAcc($isAllowOpenAcc) {
        $this->is_allow_open_acc = $isAllowOpenAcc;
    }

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

    /**
     * Set status
     *
     * @param string $status
     */
    public function setStatus($status) {
        $this->status = $status;
    }

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

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

    /**
     * Get created_at
     *
     * @return \DateTime 
     */
    public function getCreatedAt() {
        return $this->created_at;
    }

    /**
     * Set layer
     *
     * @param integer $layer
     * @return User
     */
    public function setLayer($layer) {
        $this->layer = $layer;

        return $this;
    }

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

    /**
     * Set selected_package
     *
     * @param integer $selectedPackage
     * @return User
     */
    public function setSelectedPackage($selectedPackage) {
        $this->selected_package = $selectedPackage;

        return $this;
    }

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

    /**
     * Add children
     *
     * @param \MyBundle\Entity\User $children
     * @return User
     */
    public function addChild(\MyBundle\Entity\User $children) {
        $this->children[] = $children;

        return $this;
    }

    /**
     * Remove children
     *
     * @param \MyBundle\Entity\User $children
     */
    public function removeChild(\MyBundle\Entity\User $children) {
        $this->children->removeElement($children);
    }

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

    /**
     * Add package
     *
     * @param \MyBundle\Entity\Package $package
     * @return User
     */
    public function addPackage(\MyBundle\Entity\Package $package) {
        $this->packages[] = $package;

        return $this;
    }

    /**
     * Remove package
     *
     * @param \MyBundle\Entity\Package $package
     */
    public function removePackage(\MyBundle\Entity\Package $package) {
        $this->packages->removeElement($package);
    }

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

    /**
     * Set packages
     *
     * @param Collection $packages
     * @return User
     */
    public function setPackages($packages) {
        $this->packages = $packages;

        return $this;
    }

    /**
     * Set cms_bet
     *
     * @param integer $cmsBet
     * @return User
     */
    public function setCmsBet($cmsBet) {
        $this->cms_bet = $cmsBet;

        return $this;
    }

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

    /**
     * Set cms_prize
     *
     * @param integer $cmsPrize
     * @return User
     */
    public function setCmsPrize($cmsPrize) {
        $this->cms_prize = $cmsPrize;

        return $this;
    }

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

}

Below are dump of loaded user object (current user): enter image description here

Below are dump of new user object: enter image description here

The error log:

CRITICAL - Uncaught PHP Exception Symfony\Component\Debug\Exception\ContextErrorException: "Warning: spl_object_hash() expects parameter 1 to be object, integer given" at C:\xampp\htdocs\project\vendor\doctrine\orm\lib\Doctrine\ORM\UnitOfWork.php line 1389

The different thing I can see is the data type of $child, $roles and $packages variable. Does it make any different? For variable child I didn't set anything because it is not belong to the variable to database. It is only for self-referencing usage, $parent_id has the self-referencing relationship with this $child so I only set $parent_id.

I really got no idea, maybe I have wrong concept about the self-referencing.

Thank your for your help.

1条回答
三岁会撩人
2楼-- · 2019-09-08 15:09

Looking at the annotations, my guess is that User::setParentId() is expecting a User object, not an integer:

  /**
     * @ORM\ManyToOne(targetEntity="User", inversedBy="children")
     * @ORM\JoinColumn(name="parent_id", referencedColumnName="id", nullable=true)
     * 
     */

So instead of passing

$new_user->setParentId($currentuser->getId());

try:

$new_user->setParentId($currentuser);

If that works, the field name should be changed appropriately to something like parent instead of parent_id.

查看更多
登录 后发表回答