我有两个表,nodes
和terms
。DataMapper通过外键获取值
在nodes
相关字段是:nid
(主键)和value
在terms
,它们是:value
,tid
,和nid
,其中value
和tid
一起是主键和nid
是一个外键引用nodes.nid
。我想将记录添加到terms
。我有tid
和nid
,以及value
我想从相应的node
--例如查询value
对于给定的nid
node
,然后将其作为value
在terms
中。
办法做到这在SQL可能是:
INSERT INTO terms(tid, nid, value)
values(mytid, mynid, (
select value from nodes where nid=mynid
));
有人能帮助我的DataMapper做到这一点?
class Node
include DataMapper::Resource
property :nid, Serial, :key => true
property :value, Integer
end
class Term
include DataMapper::Resource
property :tid, Integer, :key => true
# how do I define nid and value?
end
# and then what do I give to Term.new or Term.create and how?
如果任何人都可以指点我一个DataMapper的好教程,我会很感激。我一直在使用他们的在线文档,但我发现我发现自己的情况很少覆盖。
我找到了文档的OK,但支持在IRC上很棒。看看我有下面的问题。答案解释了如何对他们进行关系和查询。 http://stackoverflow.com/questions/2826439/beginning-with-datamapper-association-question – 2011-04-05 19:02:32