嗨,我对 OneToOne 关系有一个小问题,当它运行以下内容时:-
$userToView = $this->view->entityManager->getRepository("Ajfit\Entity\User")
->findOneByName($userName);
这会按预期填充,但名为engineerFk 的关系字段是代理实体,其标识符字段设置为[],但值设置正确,见下文:-
我的用户实体是这样的:-
class User extends PersistentObject
{
/**
* @var integer $pk
*
* @ORM\Column(name="pk", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
protected $pk;
/**
* @var integer $engineerFk
* @ORM\OneToOne(targetEntity="Ajfit\Entity\Engineer")
* @ORM\JoinColumn(name="pk", referencedColumnName="user_fk")
*/
protected $engineerFk;
}
我的工程师实体设置如下:-
class Engineer extends PersistentObject
{
/**
* @var integer $pk
*
* @ORM\Column(name="pk", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
}
protected $pk;
我在数据库中的 sql 是这样的:-
/*user table*/
CREATE TABLE `user` (
`pk` int(10) unsigned NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`pk`)
) ENGINE=InnoDB AUTO_INCREMENT=34 DEFAULT CHARSET=utf8 COLLATE=utf8_bin
/*engineer table */
CREATE TABLE `engineer` (
`pk` int(10) unsigned NOT NULL AUTO_INCREMENT,
`user_fk` int(10) unsigned NOT NULL,
PRIMARY KEY (`pk`),
KEY `engineer_user_fk` (`user_fk`),
CONSTRAINT `engineer_user_fk` FOREIGN KEY (`user_fk`) REFERENCES `user` (`pk`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8
任何重新解决这个问题的帮助都会非常感激。
谢谢
安德鲁