我希望为每个我的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
如何实施此解决方法?有没有办法解决它?
谢谢!
答案 0 :(得分:1)
您还应设置$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);
希望这个帮助