Php 条令-一对一关系-实体不成立例外
我想指定两个实体Item和ItemPrice之间的关系。项目可以有价格,但不一定要有价格,所以它应该类似于sql中的左连接。我使用了OneToOne关系,JoinColumn和nullable=true,得到的是EntityNotFoundExceptionexception。这是我的密码: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")
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
,并将其用作联接列
您应该考虑使用创建/插入表并验证实体定义的原则,这样就不会有这些问题。
您可以提供您的命名空间定义吗?是否有任何成功解决了您的问题?