Mam jednostkę Item
, która ma relację ManyToOne
z jednostką Category
. Chcę, aby dołączyły do nich pola inne niż kategoria id
(w tym przypadku pole o nazwie id2
). Mój schemat znajduje się poniżej.Czy możliwe jest odwołanie się do kolumny innej niż "id" dla kolumny JoinColumn?
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;
gdy próbuję zapisywania Item
otrzymuję ten błąd:
Notice: Undefined index: id2 in vendor/doctrine/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php line 511
pewno wystarczy, jeśli zmienię id2
do id
w JoinColumn
adnotacji, wszystko działa dobrze, ale muszę podmiotów podlegających połączone przez id2
. czy to możliwe?
Edit
Co chcę osiągnąć jest niemożliwe według oficjalnej doktryny 2 docs.
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.
źródło: http://www.doctrine-project.org/docs/orm/2.1/en/reference/limitations-and-known-issues.html
Dzięki za odpowiedź. Próbowałem zmienić typ pola na liczbę całkowitą i dodać @Index, ale nadal nie działa. –
Bah! Wygląda na to, że w końcu muszą być fks. Zawsze jest opcja "wiele do wielu". – quickshiftin