0

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.

TheGremlyn
  • 323
  • 1
  • 3
  • 21
  • Take a look at doctrine documentation for self-referencing (OneToOne, OneToMany and ManyToMany) [http://docs.doctrine-project.org/en/2.0.x/reference/association-mapping.html#one-to-many-self-referencing] – Javad May 01 '14 at 01:09
  • @Javad, I have looked over those docs and cannot find out how to change the unique constraint. As I stated in my post, my original self-referencing OneToOne set up worked exactly as I wanted UNTIL I needed to make multiple references to the same Task. – TheGremlyn May 01 '14 at 05:19
  • Can you drop the previously created index and update the schema base on new changes? I am not sure if this may help you or not, but you can change the unique constraint by using `@ORM\Table(name="table_name", uniqueConstraints={ @ORM\UniqueConstraint(name="set_any_name", columns={"put","list","of","unique","columns"})})` which needs `use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;` – Javad May 01 '14 at 13:57
  • @Javad, I don't want to make something unique, I want to make it NOT unique. It seems that Doctrine automatically applies a uniqueness constraint to a OneToOne relationship, which in some ways makes sense. I see no way to remove that uniqueness constraint from a OneToOne relationship, but am unclear on how to apply a OneToMany or ManyToOne relationship to achieve what I desire and have described in my post. Note that I am not unclear about using the OneToMany or ManyToOne relationships in general. It is the specific use case that presenting problems for me to figure this all out. – TheGremlyn May 01 '14 at 14:37

1 Answers1

0

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.

TheGremlyn
  • 323
  • 1
  • 3
  • 21