如何通过UI5中的OdataModel.create为2个或更多实体集创建条目

时间:2018-06-19 04:35:26

标签: odata sapui5

我有一个有4种实体类型的Odata服务 - 地址,CC,标题,项目,评论。 用户按下“订单”按钮后,我想使用此服务在后端创建一个条目。我需要传递一个标题和相关项目。

来自后端的oData服务:

{
  "d" : {
    "__metadata" : {
      "id" : "http://.../sap/opu/odata/sap/ZOrders/HeaderSet('3898')",
      "uri" : "http://.../sap/opu/odata/sap/ZOrders/HeaderSet('3898')",
      "type" : "ZOrder.Header"
    },
    "CompanyID" : "W",
    "StockRoomID" : "A",
    "SalesDocument" : "3898",  


"ItemSet" : [
        {
          "__metadata" : {
            "id" : "http://.../sap/opu/odata/sap/ZOrders/ItemSet(SalesDocument='3898',SalesDocumentItem='000010')",
            "uri" : "http://.../sap/opu/odata/sap/ZOrders/ItemSet(SalesDocument='3898',SalesDocumentItem='000010')",
            "type" : "ZOrders.Item"
          },
          "SalesDocument" : "3898",
          "SalesDocumentItem" : "000010",  //Line item number
          "StockRoomID" : "A",
          }
      ]
   }
}

Controller.js

 buttonClick: function(event) {
    var sServiceURl = this.getOwnerComponent().getMetadata().getManifestEntry("sap.app").dataSources["ZOrders"].uri;
   this.OdataModel = new sap.ui.model.odata.v2.ODataModel(sServiceURl);
   var model = vc.getView().getModel();

   var oEntry = {};
   oEntry.CompanyID = model.getProperty("/CompanyID");
   oEntry.StockRoomID = model.getProperty("/StockRoomID");
   oEntry.SalesDocument = model.getProperty("/SalesDocument");



    //Now want to pass items data to service so 
    //Creating an empty ItemSet array, pulling data from model.

    //But my logic is wrong here for sure. 
            var itemData = [];

            var itemsArray = v.getProperty("/ItemSet");

            for (var i = 0; i < itemsArray.results.length; i++) {
            itemData.push({
                    SalesDocument: itemsArray.results[i].SalesDocument,
                    SalesDocumentItem: itemsArray.results[i].SalesDocumentItem,
                    StockRoomID: itemsArray.results[i].StockRoomID,

                });

            }
            oEntry.ItemSet = itemData;

     this.OdataModel.create("/HeaderSet", oEntry, this._submitOrderSuccess.bind(this), this._submitOrderError.bind(this));

**调试器 - 标头有效负载**

{  
   "CompanyID":"RSW0",
   "StockRoomID":"A200",
   "SalesDocument":"4053",
   "Return":"X",
   "Reason":"101",
   "ItemSet":[  
      {  
         "SalesDocument":"4053",
         "SalesDocumentItem":"000010",
         "StockRoomID":"A200",
         "ReturnItemFlag":"X",
         "QtyToReturn":"1.000"
      },
      {  
         "SalesDocument":"4053",
         "SalesDocumentItem":"000020",
         "StockRoomID":"A200",
         "ReturnItemFlag":"X",
         "QtyToReturn":"1.000"
      },
      {  
         "SalesDocument":"4053",
         "SalesDocumentItem":"000030",
         "StockRoomID":"A200",
         "ReturnItemFlag":"X",
         "QtyToReturn":"1.000"
      }
   ]
}

**调试器 - 响应**

{  
   "d":{  
      "__metadata":{  
         "id":"https://.../sap/opu/odata/sap/ZORDER/HeaderSet('60000353')",
         "uri":"https://.../sap/opu/odata/sap/ZORDER/HeaderSet('60000353')",
         "type":"ZORDER.Header"
      },
      "CompanyID":"",
      "StockRoomID":"",
      "SalesDocument":"60000353",
      "ReferenceDoc":"",
      "AccountNumber":"",
      "PoNumber":"",
      "Message":"Return order 0060000353 has been created successfully",
      "OrderTotal":"0.00",
      "StockRoomName":"",
      "Return":"",
      "Reason":"",
      "Auth":"",
      "ItemSet":null,
      "HeaderCSDSet":{  
         "__deferred":{  
            "uri":"https://.../sap/opu/odata/sap/ZORDER/HeaderSet('60000353')/HeaderCSDSet"
         }
      },
      "AddressSet":{  
         "__deferred":{  
            "uri":"https://.../sap/opu/odata/sap/ZORDER/HeaderSet('60000353')/AddressSet"
         }
      },
      "GeneralCommentsSet":{  
         "__deferred":{  
            "uri":"https://.../sap/opu/odata/sap/ZORDER/HeaderSet('60000353')/GeneralCommentsSet"
         }
      },
      "CreditCardSet":{  
         "__deferred":"uri":"https://.../sap/opu/odata/sap/ZORDER/HeaderSet('60000353')/CreditCardSet"
         }
      }
   }
}

1 个答案:

答案 0 :(得分:0)

这是从Header到Item的依赖Entity或Navigation属性的场景。 Google“SAPUI5中的深层实体和oData以便更好地理解”。

您提到的代码中存在错误:“逻辑错误”。

只需在for循环后添加一行:

oEntry.Items = ItemSet; 

如果这有帮助,请告诉我。