spl_object_hash()期望参数1为object,给定null

时间:2014-02-14 12:55:27

标签: php doctrine-orm

在一个项目中,我使用的是学说2,我有一对多的关系,如:

客户=>订单

我的问题看起来与this问题类似,只是当我尝试使用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的内容如下所示:

<?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;
    }

}

更新:以下是导致错误的代码:

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

Update2:由于客户内部的ID是User对象,因此我也尝试过以下操作:

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

我已经在this页面上尝试了答案,但这没有用!我需要做些什么来实现这个目标?

1 个答案:

答案 0 :(得分:1)

我想要添加一对多关系的实体类有一个主键,它被映射为导致问题的另一个实体的一对一关系。因此,为了解决这个问题,我确实删除了一对一的关系并更新了我的代码,使其无需一对一的映射。