Spl_object_hash () expects parameter 1 to be an object, null

In the project, I use doctrine 2, where I have from one to many relationships:

Customer => Orders

My problem is similar to this question, except that I am already getting an error when trying to get an object using 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 identifier inside the client is a User object, I also tried to follow without success:

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

I already tried to answer this page , but it didn’t help! What do I need to do to make this work?

+1
source share
1 answer

The entity class in which I wanted to add a relationship from one to another had a primary key that was mapped as a one-to-one relationship with another object that caused the problem. Thus, to get around this problem, I deleted the one-to-one relationship and updated my code to work without one-to-one mapping.

+1
source

Source: https://habr.com/ru/post/909901/


All Articles