Tôi có một thực thể Item
có mối quan hệ ManyToOne
với một thực thể Category
. Tôi muốn họ được tham gia bởi một lĩnh vực khác ngoài Hạng mục id
(trong trường hợp này, một trường có tên là id2
). Sơ đồ của tôi được liệt kê dưới đây.Có thể tham chiếu một cột khác với 'id' cho một JoinColumn không?
class Item {
/**
* @ORM\Id
* @ORM\Column(name = "id", type = "integer")
* @ORM\GeneratedValue(strategy = "AUTO")
*/
protected $id;
/**
* @ORM\ManyToOne(targetEntity = "Category")
* @ORM\JoinColumn(name = "category_id", referencedColumnName = "id2")
*/
protected $category;
}
class Category {
/**
* @ORM\Id
* @ORM\Column(name = "id", type = "integer")
* @ORM\GeneratedValue(strategy = "AUTO")
*/
protected $id;
/**
* @ORM\Column(name = "id2", type = "string", length = "255", unique = "true")
*/
protected $id2;
Khi tôi cố gắng tiết kiệm một Item
tôi nhận được lỗi này:
Notice: Undefined index: id2 in vendor/doctrine/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php line 511
Chắc chắn, nếu tôi thay đổi id2
-id
trong JoinColumn
chú thích, mọi thứ hoạt động tốt, nhưng tôi cần các đối tượng được được kết nối qua id2
. Điều này có thể không?
Chỉnh sửa
Điều tôi muốn đạt được là không thể theo Doctrine 2 tài liệu chính thức.
It is not possible to use join columns pointing to non-primary keys. Doctrine will think these are the primary keys and create lazy-loading proxies with the data, which can lead to unexpected results. Doctrine can for performance reasons not validate the correctness of this settings at runtime but only through the Validate Schema command.
nguồn: http://www.doctrine-project.org/docs/orm/2.1/en/reference/limitations-and-known-issues.html
Cảm ơn đã phản ứng. Tôi đã thử thay đổi kiểu trường thành số nguyên và thêm @Index, nhưng nó vẫn không hoạt động. –
Bah! Có vẻ như chúng cần phải là loại đồ lót. Luôn có nhiều tùy chọn. – quickshiftin