我遇到映射不一致的问题。我在我的应用程序中有两个实体 - 联系人(有联系人的实体......)和信息,包含此联系人信息的实体(电话,电子邮件,传真,网站等)。
在我的 Contact 实体中,我为每种类型创建了变量,我的应用程序中需要它,因为这种方式更容易:
/**
* @ORM\OneToMany( targetEntity = "RelationInformations" , mappedBy = "objectID", cascade={"persist"} )
*/
protected $contactInformations;
/**
* @ORM\OneToMany( targetEntity = "RelationInformations" , mappedBy = "objectID", cascade={"persist"} )
*/
protected $contactPhone;
/**
* @ORM\OneToMany( targetEntity = "RelationInformations" , mappedBy = "objectID", cascade={"persist"} )
*/
protected $contactFax;
/**
* @ORM\OneToMany( targetEntity = "RelationInformations" , mappedBy = "objectID", cascade={"persist"} )
*/
protected $contactWebsite;
/**
* @ORM\OneToMany( targetEntity = "RelationInformations" , mappedBy = "objectID", cascade={"persist"} )
*/
protected $contactEmail;
/**
* @ORM\OneToMany( targetEntity = "RelationInformations" , mappedBy = "objectID", cascade={"persist"} )
*/
protected $contactCommunicator;
例如手机的 getter 看起来像:
/**
* Get contactPhone
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getContactPhone()
{
if ($this->contactPhone !== null) {
foreach ($this->contactPhone->toArray() as &$info) {
if ($info->getType() !== RelationInformations::TYPE_TELEPHONE) {
$this->contactPhone->removeElement($info);
}
}
}
return $this->contactPhone;
}
这样我只能通过使用此功能从我的信息中获得电话,因此在应用程序的其他地方更容易获得我想要的东西。
关系信息实体:
/**
* @var integer
* @ORM\Column( name = "rnis_id" , type = "integer" , nullable = false );
* @ORM\Id
* @ORM\GeneratedValue( strategy = "AUTO")
*/
private $id;
/**
* @var integer
* @ORM\ManyToOne( targetEntity = "RelationContact" , inversedBy = "contactInformations" )
* @ORM\JoinColumn( name = "rnis_object_id" , referencedColumnName="rnct_id", nullable = false );
*/
private $objectID;
/**
* @var string
* @ORM\Column( name = "rnis_value" , type = "string" , nullable = false )
*/
private $value;
/**
* @var string
* @ORM\Column( name = "rnis_type" , type = "string" , nullable = false , length = 1 )
*/
private $type;
/**
* @var boolean
* @ORM\Column( name = "rnis_active" , type = "boolean" , nullable = false )
*/
private $active;
/**
* @var boolean
* @ORM\Column( name = "rnis_default" , type = "boolean" , nullable = false )
*/
private $default;
/**
* @var string
* @ORM\Column( name = "rnis_txt" , type = "string" , nullable = true )
*/
private $txt;
/**
* @var integer
* @ORM\Column( name = "rnis_type_private_business" , type = "integer" , nullable = true )
*/
private $typePrivateBusiness;
问题是在我的分析器中我可以看到如下错误。应用程序工作正常,但我想解决这个问题。
The mappings RelationContact#contactPhone and RelationInformations#objectID are inconsistent with each other.
The mappings RelationContact#contactFax and RelationInformations#objectID are inconsistent with each other.
The mappings RelationContact#contactWebsite and RelationInformations#objectID are inconsistent with each other.
The mappings RelationContact#contactEmail and RelationInformations#objectID are inconsistent with each other.
The mappings RelationContact#contactCommunicator and RelationInformations#objectID are inconsistent with each other.
The mappings RelationContact#contactBrand and RelationInformations#objectID are inconsistent with each other.
最佳答案
您不能将相同的 OneToMany
关系映射到相同的 mappedby
键上,因此原则映射验证的行为是正确传递第一个 contactInformations
引用并在另一个上失败。
按照 Doctrine2 doc reference 中的描述,尝试将您的实体映射为一对多、单向且带有连接表
希望对你有帮助
关于php - 映射相互不一致,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26254110/