spl_object_hash()期望参数1为对象,给定null [英] spl_object_hash() expects parameter 1 to be object, null given
问题描述
在一个项目中,我使用的是学说2,其中有一对多关系,例如:
In a project I am using doctrine 2 where I have a one to many reltionships like:
Customer => Orders
Customer => Orders
我的问题类似于这个问题,除了当我尝试使用arraycollection()检索实体时我已经得到一个错误:
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
Customer.php的内容如下所示:
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::由于客户内部的id是User对象,因此我也尝试了以下尝试,但没有成功:
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?
推荐答案
我想在其中添加一对多关系的实体类一个主键,该主键以一对一的关系映射到另一个导致问题的实体。因此,要解决此问题,我确实删除了一对一关系,并更新了我的代码以使其不使用一对一映射。
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.
这篇关于spl_object_hash()期望参数1为对象,给定null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!