简体   繁体   English

如何使用 oModel.createEntry 创建深层实体

[英]How to create deep entity with oModel.createEntry

Is there a way to create a "temporary" (or I guess it's called virtual) deep entity with using oModel.createEntity() ?有没有办法使用oModel.createEntity()创建“临时”(或者我猜它被称为虚拟)深层实体?

I have a entity called Timesheet with an association to breaks, called ToBreaks.我有一个名为 Timesheet 的实体,它与中断关联,称为 ToBreaks。

Now I want to create a new entity in the frontend by using oModel.createEntity("/TimesheetSet") .现在我想使用oModel.createEntity("/TimesheetSet")在前端创建一个新实体。

Unfortunately in the so called virtual new entry all my associations are missing.不幸的是,在所谓的虚拟新条目中,我的所有关联都丢失了。 Because I use the assications for binding tables, after creating the virtual new entry, a backend call is triggered TimesheetSet("id-04-123456789")/ToBreaks which leads to a "invalid key predicate" error.因为我使用绑定表的关联,在创建虚拟新条目后,会触发后端调用TimesheetSet("id-04-123456789")/ToBreaks这导致“无效的键谓词”错误。

Is there a way to do this with OData V2?有没有办法用 OData V2 做到这一点?

Update 09.08.2016: 2016 年 8 月 9 日更新:

You can still try by just using the properties parameter with a nested entity set.您仍然可以尝试仅使用带有嵌套实体集的 properties 参数。 As long as your OData service supports the corresponding deep create it should work:只要您的 OData 服务支持相应的深度创建,它就应该可以工作:

I've also tried something like this:我也试过这样的事情:

oModel.createEntry("/TimesheetEntry", {
    Pernr: sPernr,
    Begda: dBegda,
    ToBreaks: [{
        Pernr: sPernr,
        Begda: dBegda
    }]
});

ToBreaks is the name of the association. ToBreaks是协会的名称。

In the virtual entry of the OData-Model the properties for the associations are still missing.在 OData-Model 的虚拟条目中,关联的属性仍然缺失。 I can create the new entry by using the code above, but afterwards there is no property called ToBreaks我可以使用上面的代码创建新条目,但之后没有名为ToBreaks属性

On the Backend-Side I followed this tutorial for implementing the deep_create method: step-by-step-development-guide-for-createdeepentity-operation .在后端,我按照本教程来实现 deep_create 方法: step-by-step-development-guide-for-createdeepentity-operation I'm able to trigger the method from within the SAP Gateway Client.我可以从 SAP Gateway Client 中触发该方法。

From the API docs :来自API 文档

Please note that deep creates (including data defined by navigationproperties) are not supported.请注意,不支持深度创建(包括由导航属性定义的数据)。

You can still try by just using the properties parameter with a nested entity set.您仍然可以尝试仅使用带有嵌套实体集的properties参数。 As long as your OData service supports the corresponding deep create it should work:只要您的 OData 服务支持相应的深度创建,它就应该可以工作:

properties could be an object which includes the desired properties and the values which should be used for the created entry. properties 可以是一个对象,其中包括所需的属性和应用于创建的条目的值。

Yes you can execute deep entity creation with oData V2.是的,您可以使用 oData V2 执行深度实体创建。 In order to do so you need to build your request body according to your metadata structure.为此,您需要根据元数据结构构建请求正文。 Let's assume that your metadata model is User and each user have multiple Communication .假设您的元数据模型是User并且每个用户都有多个Communication So we have a user entity and communication entity.所以我们有一个用户实体和通信实体。 We also have association of fromUserToCommunications and navigation property let's call it Communications我们也有fromUserToCommunications和导航属性的关联,我们称之为Communications

In order to execute a call to deep create a User entity you will need to do the following:为了执行深度创建用户实体的调用,您需要执行以下操作:

// Create the oData Model
var oModel = new sap.ui.model.odata.ODataModel("{YOUR_SERVICE_DOCUMENT_URL}");


// Create the user request body, please make sure that 
// all required fields are filled in and are according to the 
// user entity metadata
var userRequestBody = {
    id: "123",
    firstName: "Your First Name",
    lastName: "Your Last Name",
    address: "Your Address",
    communications: []  // contain multiple communications 
};

var comm1 = {
    id : "1",
    userId : "123", // foregin key to the user entity
    homePhone: "+134342445435" 
};

var comm2 = {
    id : "2",
    userId : "123", // foregin key to the user entity
    homePhone: "+134342445436" 
};    

// add the communications to the user entity 
userRequestBody.communications.push(comm1);
userRequestBody.communications.push(comm2);

oModel.create("/UserCollection",userRequestBody,{
    success: function(result){
        // everything is OK 
    },
    error: function(err){
        // some error occuerd 
    },
    async: true,  // execute async request to not stuck the main thread
    urlParameters: {}  // send URL parameters if required 
}); 

You can create a deep virtual entity by creating the children and manually updating the navigation property on your parent object:您可以通过创建子对象并手动更新父对象上的导航属性来创建深度虚拟实体:

// oStartContext is a client side object representation
oStartContext = oModel.createEntry("/YourObjectSet", { properties: oYourObjectData });

aChildren is an array object of your children aChildren是您孩子的数组对象

var aChildren = [];
var aChildrenCtx = "";
var aChildrenEntries = [];

for(var i = 0; i < aChildren.length; i++) {
    var entry = aChildren[i];
    aChildrenCtx = oModel.createEntry("/YourObjectChildSet", 
        {
            properties: entry, 
            context: oStartContext
        }
    );
    aChildrenEntries.push(aChildrenCtx.getPath().substring(1));
}

Manually reset the ObjectChild_R relation to the client side object representation手动将 ObjectChild_R 关系重置为客户端对象表示

modelData.setProperty("ObjectChild_R", aChildrenEntries, oStartContext);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM