2014-10-08 14 views
6

Tôi gặp sự cố với ánh xạ không phù hợp. Tôi có trong ứng dụng của tôi hai thực thể - Liên hệ (thực thể với địa chỉ liên lạc ...) và Thông tin, thực thể với thông tin liên lạc này (điện thoại, email, fax, các trang web, vv).Các ánh xạ không phù hợp với nhau

Và Trong tổ chức Liên hệ của tôi, tôi đã biến đối với từng loại, tôi cần nó trong ứng dụng của tôi, vì cách này là dễ dàng hơn nhiều:

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

Và ví dụ getter cho điện thoại trông giống như:

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

Bằng cách này tôi chỉ nhận được điện thoại từ thông tin của tôi chỉ bằng cách sử dụng chức năng này vì vậy nó dễ dàng hơn nhiều ở những nơi khác trong ứng dụng để có được những gì tôi muốn.

RelationInformation Entity:

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

Vấn đề là trong hồ sơ của tôi, tôi có thể thấy lỗi như dưới đây. Ứng dụng hoạt động chính xác nhưng tôi muốn giải quyết vấn đề này.

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. 
+0

xin vui lòng gửi mã của Entity 'RelationInformations' – Matteo

+0

Ok, tôi có thêm nó;) –

Trả lời

8

Bạn không thể lập bản đồ OneToMany quan hệ trong cùng một mappedby chìa khóa, vì vậy beahviour của xác lập bản đồ học thuyết là phải vượt qua một cách chính xác các tài liệu tham khảo contactInformations đầu tiên và thất bại mặt khác.

Cố gắng để lập bản đồ các đơn vị bạn như One-To-Many, Unidirectional with Join Table như mô tả trong Doctrine2 doc reference

Hope trợ giúp này

+1

Bạn đã cho tôi một ý tưởng làm thế nào để giải quyết nó, tôi sẽ thử nó ngay bây giờ và tôi sẽ viết khi tôi sẽ biết điều gì đó hơn ;-) –

+0

Mọi thứ trông okey nhưng ... Khi tôi có trong bộ sưu tập biểu mẫu của tôi ánh xạ trên 'contactPhone' ví dụ. Điện thoại xuất hiện chính xác nhưng khi tôi muốn thêm điện thoại, tôi có lỗi trong 'ClassMetadata' không có khóa nào như contactPhone (vì hiện tại không có quan hệ nào nên bộ sưu tập này không phải là 'tự nhiên'). Matteo có ý kiến ​​gì không? :-) –

+1

Xem xét các mối quan hệ doctrine2 có tên là 'Một-Nhiều, Một chiều với Bảng Tham gia' và xem có đáp ứng các yêu cầu của bạn hay không. – Matteo

Các vấn đề liên quan