我想对我的每个 Doctrine 2 实体进行关系引用的替代方法:每个实体关系都可以通过 id 字段或引用字段实例化,并且两个字段映射到同一个数据库列。
让我展示一下:
/**
* @var integer
*
* @Column(name = "REF_ID", type = "integer", nullable = false)
*/
private $refId;
/**
* @var IBlock
*
* @ManyToOne(targetEntity = "Ref")
* @JoinColumn(name = "REF_ID", referencedColumnName = "ID")
*/
private $ref;
我呼吁坚持这样的实体:
$entity = new Entity();
$entity->setName("Test entity");
$entity->setRefId(14);
我收到以下错误:
Column 'REF_ID' cannot be null
如何实现解决方法?有什么办法可以解决它吗?
谢谢!
您还应该设置 $ref
属性,例如:
// Retrieve the $ref entity object as example with the repository
$ref = $refRepo->find(14)
//or
// $ref = $em->getRepository('Entity')->find($id);
// as described here in the doc
// http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/working-with-objects.html#by-primary-key
$entity = new Entity();
$entity->setName("Test entity");
$entity->setRefId(14);
$entity->setRef($ref);
希望这个帮助