【问题标题】:The mappings are inconsistent with each other映射不一致
【发布时间】:2014-12-02 22:46:09
【问题描述】:

我遇到了不一致的映射问题。我在我的应用程序中有两个实体 - 联系人(具有联系人的实体......)和信息,具有此联系人信息的实体(电话、电子邮件、传真、网站等)。

在我的联系人实体中,我为每种类型创建了变量,我需要在我的应用程序中使用它,因为这种方式更容易:

/**
 * @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;
}

通过这种方式,我只能通过使用此功能从我的信息中获取电话,因此在应用程序的其他地方更容易获得我想要的东西。

RelationInformation 实体:

   /**
     * @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.

【问题讨论】:

  • 请贴出实体代码RelationInformations
  • 好的,我已经添加了;)

标签: php database symfony doctrine-orm mapping


【解决方案1】:

您不能将相同的OneToMany 关系映射到相同的mappedby 键上,因此原则映射验证的行为是正确地通过第一个contactInformations 引用并在另一个上失败。

尝试将您的实体映射为One-To-Many, Unidirectional with Join Table,如Doctrine2 doc reference 中所述

希望有帮助

【讨论】:

  • 你给了我一个解决方法的想法,我现在就试试,等我知道的时候再写;-)
  • 一切看起来都不错,但是...例如,当我在表单集合中映射到“contactPhone”时。电话正确显示,但是当我想添加电话时,我有一个错误,即在“ClassMetadata”中没有像contactPhone 这样的键(因为现在没有关系,所以这个集合不是“自然的”)。有什么想法吗? :-)
  • 查看名为One-To-Many, Unidirectional with Join Table的doctrine2关系,看看是否满足您的要求。
  • 正如我所见,它只会为关系创建另一个表,或者我看不到任何东西......?
  • 如文档中所述,这将enforces the one-to-many cardinality 所以我希望这可以修复学说映射检查
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-11-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-10
相关资源
最近更新 更多