开发者

DDD, ERD, ORM: has one, or belongs to one?

I was wondering if there was such thing as 'belongs to' relationship, and what was its function, or if it was just the opposite to 'has one'?

The question arised when building an ORM, and wanted to find a way to determine when a entity instance should be automatically deleted, eg:

User 'has many' Thread

Thread 'has many' Comment

Thread 'has one' User

Comment 'has one' Thread

Suppose you delete a user instance. Its related thread instances should remain intact. But if you delete a thread instance, its comments should be deleted.

With the above schema, the ORM can't tell when to delete and when not to. But if I use 'belong to', it could be a solution:

User 'has many' Thread

Thread 'has many' Comment

Thread 'has one' User

Comment 'belongs to one' Thread

Does this make sense? It is possible for the ORM, but does ERD cover this scenario? The one-to-one/one-to-many connections don't seem to be enough.

开发者_如何学CAny thoughts?


The general implementations that I have seen all provide for a "belongs to" relationship. Technically this will exist in any "Has many" relationship because the foreign key will exist in the child table.

A definition from Kohana's ORM class documentation suggests that whether a one-to-one relationship is "has one" or "belongs to" depends on the location of the foreign key field. If it is in the same table as the core model then it is a "belongs to" relationship. If it is in another table then it is a "has one" relationship.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜