Php 条令-一对一关系-实体不成立例外

Php 条令-一对一关系-实体不成立例外,php,doctrine-orm,zend-framework2,Php,Doctrine Orm,Zend Framework2,我想指定两个实体Item和ItemPrice之间的关系。项目可以有价格,但不一定要有价格,所以它应该类似于sql中的左连接。我使用了OneToOne关系,JoinColumn和nullable=true,得到的是EntityNotFoundExceptionexception。这是我的密码: class Item { /** * @ORM\Column(type="integer") * @ORM\Id * @ORM\GeneratedValue(strategy="IDENTITY")

我想指定两个实体Item和ItemPrice之间的关系。项目可以有价格,但不一定要有价格,所以它应该类似于sql中的左连接。我使用了OneToOne关系,JoinColumn和nullable=true,得到的是EntityNotFoundExceptionexception。这是我的密码:

class Item
{

/**
 * @ORM\Column(type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="IDENTITY")
 */
protected $item_id;

/**
 * @ORM\Column(type="string")
 */
protected $name;

/**
 * @ORM\Column(type="string")
 */
protected $image_file;

/**
 * @ORM\OneToOne(targetEntity="ItemPrice")
 * @ORM\JoinColumn(name="item_id", referencedColumnName="item_id", nullable=true)
 */
protected $price;
...
}


class ItemPrice
{

/**
 * @ORM\Column(type="integer")
 * @ORM\Id
 */
protected $item_id;

/**
 * @ORM\Column(type="integer")
 */
protected $gold;

/**
 * @ORM\Column(type="integer")
 */
protected $silver;

/**
 * @ORM\Column(type="integer")
 */
protected $bronze;
...
}
表:

CREATE TABLE `item` (
`item_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(150) NOT NULL,
`image_file` varchar(100) DEFAULT NULL,
PRIMARY KEY (`item_id`),
UNIQUE KEY `uk_item_name` (`name`));

CREATE TABLE `item_price` (
`item_id` int(11) NOT NULL,
`gold` int(11) NOT NULL DEFAULT '0',
`silver` int(11) NOT NULL DEFAULT '0',
`bronze` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`item_id`));
我想我所做的被称为单向一通:
在我看来,这正是我所需要的。

我认为您的映射是错误的

您可以为您的
item\u id
在您的
OneToOne
ItemPrice
关联中的join列编写
nullable=true
。这意味着如果
item\u id
null
则没有关联的ItemPrice。但是
item\u id
是主id列,因此将始终有一个值,因此ORM将始终尝试为
item
查找
item的
price
,从而导致EntityNotFoundException 您应该将
ItemPrice
设置为关系的拥有方,或者在
ItemPrice
中设置
price\u id
,并将其用作联接列


您应该考虑使用创建/插入表并验证实体定义的原则,这样就不会有这些问题。

您可以提供您的命名空间定义吗?是否有任何成功解决了您的问题?