0

我想要一个具有不同类型的模型has n,例如:

class Blog
  include DataMapper::Resource

  property :id, Serial

  has 1, :owner   # of type user...
  has n, :authors # of type user...
end

class User
  include DataMapper::Resource

  property :id, Serial

  has n, :blogs # owns some number
  has n, :blogs # is a member of some number
end

但是,我不想使用Discriminator,从那以后我需要为旧的 User 对象创建新的 Owner 或 Author 对象,这太荒谬了。

我怎样才能最好地做到这一点?

4

1 回答 1

0

尝试这个:

class User
  include DataMapper::Resource

  property :id, Serial

  has n, :blog_authors, 'BlogAuthor'
  has n, :authored_blogs, 'Blog', :through => :blog_authors, :via => :blog

  has n, :blog_owners, 'BlogOwner'
  has n, :owned_blogs, 'Blog', :through => :blog_owners, :via => :blog
end

class Blog
  include DataMapper::Resource

  property :id, Serial

  has n, :blog_authors, 'BlogAuthor'
  has n, :authors, 'User', :through => :blog_authors

  has 1, :blog_owner, 'BlogOwner'
end

class BlogAuthor
  include DataMapper::Resource

  belongs_to :blog, :key => true
  belongs_to :author, 'User', :key => true
end

class BlogOwner
  include DataMapper::Resource

  belongs_to :blog, :key => true
  belongs_to :owner, 'User', :key => true
end
于 2013-07-10T15:39:44.323 回答