我有兩個表,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