spl_object_hash() expects parameter 1 to be object

2019-08-07 02:15发布

In a project I am using doctrine 2 where I have a one to many reltionships like:

Customer => Orders

My problem looks similiar to this question except that I already get an error when I try to retrieve the entity with the arraycollection ():

Warning: spl_object_hash() expects parameter 1 to be object, null given in C:\xampp\htdocs\test.example.com\vendor\doctrine\orm\lib\Doctrine\ORM\UnitOfWork.php on line 2852

The content of Customer.php looks something like this:

<?php

namespace Application\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Customer
 *
 * @ORM\Table(name="customer", uniqueConstraints={@ORM\UniqueConstraint(name="foo", columns={"foo"})})
 * @ORM\Entity
 */
class Customer
{

    public function __construct() {
        $this->orders = new \Doctrine\Common\Collections\ArrayCollection();
    }

    /**
     * @var \Application\Entity\User
     *
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="NONE")
     * @ORM\OneToOne(targetEntity="Application\Entity\User")
     * @ORM\JoinColumns({
     *   @ORM\JoinColumn(name="id", referencedColumnName="id")
     * })
     */
    private $id;


    /**
     * 
     * @var \Doctrine\Common\Collections\ArrayCollection
     * 
     * @ORM\OneToMany(targetEntity="Order", mappedBy="customer", fetch="EAGER")
     * 
     */
    private $orders;


    /**
     * @return \Doctrine\Common\Collections\ArrayCollection a list of orders related to the customer
     */
    public function getOrders() {
        return $this->orders;
    }

    /**
     * Set id
     *
     * @param \Application\Entity\User $id
     * @return Customer
     */
    public function setId(\Application\Entity\User $id) {
        $this->id = $id;

        return $this;
    }

    /**
     * Get id
     *
     * @return \Application\Entity\User 
     */
    public function getId() {
        return $this->id;
    }

}

Update: And here is the code causing the error:

$entityManager->getRepository('\Application\Entity\Customer')->find($user->getId());//debugger shows $user->id = 7 so that isn't causing the problem

Update2: Since the id inside customer is a User object, I also tried following without success:

$entityManager->getRepository('\Application\Entity\Customer')->find($user);

I already tried the answer on this page, but that didn't help! What do I need to do to get this working?

1条回答
聊天终结者
2楼-- · 2019-08-07 03:00

The entity class where I wanted to add a one to many relationship had a primary key which was mapped as a one to one relation to another entity which was causing the problem. So to work around this problem I did remove the one to one relationship and updated my code to work without the one to one mapping.

查看更多
登录 后发表回答