shaqb4
shaqb4

Reputation: 283

Doctrine: Should this be a Many-to-Many?

So I'm using Symfony2 with Doctrine and am unsure of a relationship. The goal of the site is to allow users to browse and upload stories. A feature I'm trying to implement is that users can extend other stories. For example, if I like a fantasy story and want to write a story set in the same world, I can extend it so that it is a Child of the original Parent story. There is no limit to how many parents or children a story can have.

I thought at first that it would be a self referencing many-to-many relationship, with property called $parents and one called $children, but I'm not sure how I would actually implement that. More conceptually than in terms of code.

What I have now is (in Story.orm.yml):

parents:
  targetEntity: Story
    inversedBy: children
    joinTable:
      name: parents
      joinColumns:
        child_id:
          referencedColumnName: id
      inverseJoinColumn:
        parent_id:
          referencedColumnName: id

Is that a correct implementation?

Are there any other alternatives to a many-to-many relationship that would be better suited to something like this?

Thanks for the help!

Upvotes: 0

Views: 52

Answers (1)

Serge Kvashnin
Serge Kvashnin

Reputation: 4491

It looks like you should have OneToMany self-referencing relationship. I think so because I'm suppose that you are extending ONE story (parent), and it can has MANY (child) stories. As you say:

it is a Child of the original Parent story

It looks like single inheritance (not multiple).

Upvotes: 2

Related Questions