title: Symfony2 Inserting Error "Warning: spl_object_hash() expects parameter..."
body: <p>when I try to persist my object and flush it I get this error message:</p>
  
  <blockquote>
    <p>Warning: spl_object_hash() expects parameter 1 to be object, integer
    given 500 Internal Server Error - ContextErrorException</p>
  </blockquote>
  
  <p>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.</p>
  
  <p><strong>Below are my code for to persist the user class:</strong></p>
  
  <pre><code>$package = $em-&gt;getRepository('MyBundle:Package')-&gt;findOneBy(array('id' =&gt; 1));
  $new_user-&gt;addPackage($package);
  
  $role = $em-&gt;getRepository('MyBundle:Role')-&gt;findOneBy(array('id' =&gt; 3));
  $new_user-&gt;addRole($role);
  
  $new_user-&gt;setCmsPrize(2); //int
  $new_user-&gt;setCmsBet(3); //int
  $new_user-&gt;setName("new user");
  $new_user-&gt;setUserName("test123");
  $new_user-&gt;setPassword("abc");
  $new_user-&gt;setCreditLimit(1000);
  $new_user-&gt;setCreditBalance(2000);
  $new_user-&gt;setSelectedPackage($currentuser-&gt;getSelectedPackage());
  $new_user-&gt;setParentId($currentuser-&gt;getId());
  $new_user-&gt;setLayer($currentuser-&gt;getLayer() + 1);
  $em-&gt;persist($new_user);
  $em-&gt;flush();
  </code></pre>
  
  <p><strong>Below is my user class:</strong></p>
  
  <pre><code>/**
   * 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-&gt;isActive = true;
          $this-&gt;roles = new ArrayCollection();
          $this-&gt;packages = new ArrayCollection();
          $this-&gt;created_at = new \DateTime('NOW');//date('Y-m-d H:i:s');
          $this-&gt;credit_balance = 0;
          $this-&gt;credit_limit = 0;
          $this-&gt;status = 'ACTIVE';
          $this-&gt;is_allow_open_acc = true;
          $this-&gt;children = new ArrayCollection();
          // may not be needed, see section on salt below
          // $this-&gt;salt = md5(uniqid(null, true));
      }
  
      /**
       * @inheritDoc
       */
      public function getUsername() {
          return $this-&gt;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-&gt;password;
      }
  
      /**
       * @inheritDoc
       */
      public function getRoles() {
          return $this-&gt;roles-&gt;toArray();
      }
  
      /**
       * @inheritDoc
       */
      public function eraseCredentials() {
  
      }
  
      /**
       * @see \Serializable::serialize()
       */
      public function serialize() {
          return serialize(array(
              $this-&gt;id,
              $this-&gt;username,
              $this-&gt;password,
              $this-&gt;name,
              $this-&gt;is_allow_open_acc,
              $this-&gt;created_at,
              $this-&gt;credit_balance,
              $this-&gt;credit_limit,
              $this-&gt;parent_id,
              $this-&gt;status,
              $this-&gt;cms_bet,
              $this-&gt;cms_prize,
                  // see section on salt below
                  // $this-&gt;salt,
          ));
      }
  
      /**
       * @see \Serializable::unserialize()
       */
      public function unserialize($serialized) {
          list (
                  $this-&gt;id,
                  $this-&gt;username,
                  $this-&gt;password,
                  $this-&gt;name,
                  $this-&gt;is_allow_open_acc,
                  $this-&gt;created_at,
                  $this-&gt;credit_balance,
                  $this-&gt;credit_limit,
                  $this-&gt;parent_id,
                  $this-&gt;status,
                  $this-&gt;cms_bet,
                  $this-&gt;cms_prize,
                  // see section on salt below
                  // $this-&gt;salt
                  ) = unserialize($serialized);
      }
  
      /**
       * Get id
       *
       * @return integer 
       */
      public function getId() {
          return $this-&gt;id;
      }
  
      /**
       * Set username
       *
       * @param string $username
       */
      public function setUsername($username) {
          $this-&gt;username = $username;
      }
  
      /**
       * Set password
       *
       * @param string $password
       */
      public function setPassword($password) {
          $this-&gt;password = $password;
      }
  
      /**
       * Get isActive
       *
       * @return boolean 
       */
      public function getIsActive() {
          if ($this-&gt;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-&gt;getIsActive();
      }
  
      /**
       * Add roles
       *
       * @param \MyBundle\Entity\Role $roles
       */
      public function addRole(\MyBundle\Entity\Role $roles) {
          $this-&gt;roles[] = $roles;
      }
  
      /**
       * Remove roles
       *
       * @param \MyBundle\Entity\Role $roles
       */
      public function removeRole(\MyBundle\Entity\Role $roles) {
          $this-&gt;roles-&gt;removeElement($roles);
      }
  
      /**
       * Set name
       *
       * @param string $name
       * @return User
       */
      public function setName($name) {
          $this-&gt;name = $name;
  
          return $this;
      }
  
      /**
       * Get name
       *
       * @return string 
       */
      public function getName() {
          return $this-&gt;name;
      }
  
      /**
       * Set parent_id
       *
       * @param integer $parentId
       */
      public function setParentId($parentId) {
          $this-&gt;parent_id = $parentId;
      }
  
      /**
       * Get parent_id
       *
       * @return integer 
       */
      public function getParentId() {
          return $this-&gt;parent_id;
      }
  
      /**
       * Set credit_limit
       *
       * @param float $creditLimit
       */
      public function setCreditLimit($creditLimit) {
          $this-&gt;credit_limit = $creditLimit;
      }
  
      /**
       * Get credit_limit
       *
       * @return float 
       */
      public function getCreditLimit() {
          return $this-&gt;credit_limit;
      }
  
      /**
       * Set credit_balance
       *
       * @param float $creditBalance
       */
      public function setCreditBalance($creditBalance) {
          $this-&gt;credit_balance = $creditBalance;
      }
  
      /**
       * Get credit_balance
       *
       * @return float 
       */
      public function getCreditBalance() {
          return $this-&gt;credit_balance;
      }
  
      /**
       * Set is_allow_open_acc
       *
       * @param boolean $isAllowOpenAcc
       */
      public function setIsAllowOpenAcc($isAllowOpenAcc) {
          $this-&gt;is_allow_open_acc = $isAllowOpenAcc;
      }
  
      /**
       * Get is_allow_open_acc
       *
       * @return boolean 
       */
      public function getIsAllowOpenAcc() {
          return $this-&gt;is_allow_open_acc;
      }
  
      /**
       * Set status
       *
       * @param string $status
       */
      public function setStatus($status) {
          $this-&gt;status = $status;
      }
  
      /**
       * Get status
       *
       * @return string 
       */
      public function getStatus() {
          return $this-&gt;status;
      }
  
      /**
       * Set created_at
       *
       * @param \DateTime $createdAt
       */
      public function setCreatedAt($createdAt) {
          $this-&gt;created_at = $createdAt;
      }
  
      /**
       * Get created_at
       *
       * @return \DateTime 
       */
      public function getCreatedAt() {
          return $this-&gt;created_at;
      }
  
      /**
       * Set layer
       *
       * @param integer $layer
       * @return User
       */
      public function setLayer($layer) {
          $this-&gt;layer = $layer;
  
          return $this;
      }
  
      /**
       * Get layer
       *
       * @return integer 
       */
      public function getLayer() {
          return $this-&gt;layer;
      }
  
      /**
       * Set selected_package
       *
       * @param integer $selectedPackage
       * @return User
       */
      public function setSelectedPackage($selectedPackage) {
          $this-&gt;selected_package = $selectedPackage;
  
          return $this;
      }
  
      /**
       * Get selected_package
       *
       * @return integer 
       */
      public function getSelectedPackage() {
          return $this-&gt;selected_package;
      }
  
      /**
       * Add children
       *
       * @param \MyBundle\Entity\User $children
       * @return User
       */
      public function addChild(\MyBundle\Entity\User $children) {
          $this-&gt;children[] = $children;
  
          return $this;
      }
  
      /**
       * Remove children
       *
       * @param \MyBundle\Entity\User $children
       */
      public function removeChild(\MyBundle\Entity\User $children) {
          $this-&gt;children-&gt;removeElement($children);
      }
  
      /**
       * Get children
       *
       * @return \Doctrine\Common\Collections\Collection 
       */
      public function getChildren() {
          return $this-&gt;children;
      }
  
      /**
       * Add package
       *
       * @param \MyBundle\Entity\Package $package
       * @return User
       */
      public function addPackage(\MyBundle\Entity\Package $package) {
          $this-&gt;packages[] = $package;
  
          return $this;
      }
  
      /**
       * Remove package
       *
       * @param \MyBundle\Entity\Package $package
       */
      public function removePackage(\MyBundle\Entity\Package $package) {
          $this-&gt;packages-&gt;removeElement($package);
      }
  
      /**
       * Get packages
       *
       * @return \Doctrine\Common\Collections\Collection 
       */
      public function getPackages() {
          return $this-&gt;packages;
      }
  
      /**
       * Set packages
       *
       * @param Collection $packages
       * @return User
       */
      public function setPackages($packages) {
          $this-&gt;packages = $packages;
  
          return $this;
      }
  
      /**
       * Set cms_bet
       *
       * @param integer $cmsBet
       * @return User
       */
      public function setCmsBet($cmsBet) {
          $this-&gt;cms_bet = $cmsBet;
  
          return $this;
      }
  
      /**
       * Get cms_bet
       *
       * @return integer 
       */
      public function getCmsBet() {
          return $this-&gt;cms_bet;
      }
  
      /**
       * Set cms_prize
       *
       * @param integer $cmsPrize
       * @return User
       */
      public function setCmsPrize($cmsPrize) {
          $this-&gt;cms_prize = $cmsPrize;
  
          return $this;
      }
  
      /**
       * Get cms_prize
       *
       * @return integer 
       */
      public function getCmsPrize() {
          return $this-&gt;cms_prize;
      }
  
  }
  </code></pre>
  
  <p><strong>Below are dump of loaded user object (current user):</strong>
  <img src="https://i.stack.imgur.com/V7tsJ.png" alt="enter image description here"></p>
  
  <p><strong>Below are dump of new user object:</strong>
  <img src="https://i.stack.imgur.com/szUdQ.png" alt="enter image description here"></p>
  
  <p><strong>The error log:</strong></p>
  
  <blockquote>
    <p>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</p>
  </blockquote>
  
  <p>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.</p>
  
  <p>I really got no idea, maybe I have wrong concept about the self-referencing.</p>
  
  <p>Thank your for your help.</p>