如何解决获取顶点中子对象的父对象id
我有两个对象(战舰和资源)。两者都是自定义对象。现在我想通过 VF 页面添加一个新资源。 resource__c 是 Warships__c(查找关系)的孩子。问题是我想插入一个新的 resource__c 记录,但我找不到 warship__c 对象的 id,就像(联系人对象中的 AccountId)
Resource__c con = new Resource__c(Name=resName,Quantity__c=Quantity,Utilization__c=Utilize,Warship__c.id=id);
// Warship__c.id or Warship__r.id etc??
insert con;
详情请查看代码
public class WarshipManagerController {
public String resName{get;set;}
public Integer Quantity{get;set;}
public Integer Utilize{get;set;}
//public List<Resource__c>resList {get;set;}
public List<Warship__c>resList {get;set;}
public List<Resource__c>newResList {get;set;}
public string id;
ApexPages.StandardController sc;
public WarshipManagerController(ApexPages.StandardController sc){
newResList =new List<Resource__c>();
this.sc = sc;
string id=ApexPages.CurrentPage().getparameters().get('id');
resList= new List <Warship__c>();
for(Warship__c Ws:[SELECT Id,(SELECT Id,Name,Utilization__c,Quantity__c FROM Resources__r)
FROM Warship__c where Id = :id] ){
for( Resource__c Rs:Ws.Resources__r){
newResList.add(Rs);
}
system.debug('---->'+newResList);
}
}
public pageReference Save(){
Resource__c con = new Resource__c(Name=resName,Resources.Warship__r=id);
insert con;
return null;
}
}
VF 页面
<apex:page standardController="Warship__c" extensions="WarshipManagerController">
<apex:form >
<apex:pageBlock title="Resource">
<apex:pageBlockSection >
First Name <apex:inputText value="{!resName}" />
Last Name <apex:inputText value="{!Quantity}" />
Phone <apex:inputText value="{!Utilize}" />
<apex:commandButton value="save" action="{!save}" />
</apex:pageBlockSection>
<apex:dataTable value="{!newResList}" var="re" width="100%">
<apex:column >
<apex:facet name="header"><b>Resource</b></apex:facet>
<apex:outputField value="{!re.Name}" />
</apex:column>
<apex:column >
<apex:facet name="header"><b>Quantity</b></apex:facet>
<apex:outputField value="{!re.Quantity__c}" />
</apex:column>
<apex:column >
<apex:facet name="header"><b>utilization</b></apex:facet>
<apex:outputField value="{!re.Utilization__c}" />
</apex:column>
</apex:dataTable>
</apex:pageBlock>
</apex:form>
</apex:page>
那么我怎样才能像 Contact To Account 一样向 Warship__C 对象插入许多 reource__c 记录
解决方法
您是否接受过普通关系数据库方面的培训?在 Resource__c 表中 Warship__c 列是 Id 列。它保存对另一个表的引用(外键)。如果您键入 Warship__c
,则表示要使用 ID。如果您键入 Warship__r.Name
,您会跳过关系到下一个表。您在数据库中进行 JOIN,Warship__r 成为表别名。
因此,如果您的 string id
持有军舰 ID(顺便说一句,这是一个糟糕的主意,永远不要将变量与类型名称相同。Id
是一种类型,您可以很好地搞砸代码. 想想Integer Account = 5; // happy debugging suckers
)
那么你应该能够做到new Resource__c(Name=resName,Quantity__c=Quantity,Utilization__c=Utilize,Warship__c=id);
大多数情况下,您将仅使用 Id 字段作为简单的引用。有时您会在插入/更新期间使用 Warship__r,但这实际上是 upsert 操作和一些高级主题。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。