문제

I have a Tasks entity with an 'id' and a 'dependency' column. My current set up has 'dependency' in a OneToOne self-referencing relationship with 'id'. The idea is that each task can have a single dependency assigned, however that dependency is not unique to just one Task.

For example, Task 4 and Task 5 can both have a dependency of Task 2, but 4 and 5 don't need to know anything about each other.

Everything worked fine until I attempted to assign the same Task as a dependency to two different, subsequent Tasks. It would seem that OneToOne relationships are defined as unique and that is not something that can be changed. This brings me to trying to figure out the best way of implementing what I want. I want a unidirectional relationship, as the preceding Task has no need to know about the subsequent Tasks that depend on it.

ManyToOne doesn't seem to make sense since a Task can only have one dependency. OneToMany gives information to the 'id' column about dependent Tasks, which is doesn't need and I'm not sure if that would have unforeseen ramifications down the road when updating entities?

Maybe I'm not understanding things quite right (still fairly new to Doctrine), so if someone can point me in the right direction, I would appreciate it.

도움이 되었습니까?

해결책

Although it seemed very counter intuitive to go this route, I ended up using a ManyToMany unidirectional self-referencing relationship to accomplish my goal. Since I didn't want to mess with the 'id' column at all (it's the primary, unique key, auto incrementing), I couldn't go with a bi-directional relationship. This seemed to not be possible the way I wanted with ManyToOne (maybe I was doing it wrong, but I couldn't get it), but it was possible with ManyToMany.

Who knows, maybe in the future I will actually want to use multiple dependencies, so this does give me a little flexibility in that respect.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top