【问题标题】:SAPUI5 Metadata.xml : binding two entitiesSAPUI5 Metadata.xml:绑定两个实体
【发布时间】:2016-06-20 14:16:07
【问题描述】:

我在一家公司实习,我必须使用 SAPUI5 框架编写 JS 代码。这对我来说是全新的,这就是我遵循http://sapui5.hana.ondemand.com 教程的原因。但是现在我有一个问题,我在网上找不到任何建议,而且我不能总是让我的同事帮助我,他们也有工作......

我必须与许多员工及其任务一起制定计划。我使用带有 metadata.xml 文件的模拟服务器:

<edmx:Edmx xmlns:edmx="http://schemas.microsoft.com/ado/2007/06/edmx"
Version="1.0">
<edmx:DataServices
    xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata"
    m:DataServiceVersion="1.0">
    <Schema xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices"
        xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata"
        xmlns="http://schemas.microsoft.com/ado/2008/09/edm" Namespace="NorthwindModel">
        <EntityType Name="Employee">
            <Key>
                <PropertyRef Name="EmployeeID" />
            </Key>
            <Property xmlns:p8="http://schemas.microsoft.com/ado/2009/02/edm/annotation"
                Name="EmployeeID" Type="Edm.Int32" Nullable="false"
                p8:StoreGeneratedPattern="Identity" />
            <Property Name="LastName" Type="Edm.String" Nullable="false"
                MaxLength="20" Unicode="true" FixedLength="false" />
            <Property Name="City" Type="Edm.String" Nullable="false"/>
            <Property Name="Team" Type="Edm.String" Nullable="false"/>
            <NavigationProperty Name="Appointments" Relationship="NorthwindModel.FK_Employees_Appointment"
                FromRole="Employees" ToRole="Appointments"/>
        </EntityType>
        <EntityType Name="Appointment">
            <Key>
                <PropertyRef Name="AppointmentID"/>
            </Key>
            <Property Name="AppointmentID" Type="Edm.Int32" Nullable="false"/>
            <Property Name="EmployeeID" Type="Edm.Int32" Nullable="false"/>
            <Property Name="start" Type="Edm.DateTime" Nullable="false"/>
            <Property Name="end" Type="Edm.DateTime" Nullable="false"/>
            <Property Name="title" Type="Edm.String" Nullable="false"/>
            <NavigationProperty Name="Employees" Relationship="NorthwindModel.FK_Employees_Appointment"
                ToRole="Employees" FromRole="Appointments"/>
        </EntityType>
        <Association Name="FK_Employees_Appointment">
            <End Type="Employee" Role="Employees" Multiplicity="1" />
            <End Type="Appointment" Role="Appointments" Multiplicity="*" />
            <ReferentialConstraint>
                <Principal Role="Employees">
                    <PropertyRef Name="EmployeeID" />
                </Principal>
                <Principal Role="Appointments">
                    <PropertyRef Name="EmployeeID" />
                </Principal>
            </ReferentialConstraint>
        </Association>
    </Schema>
    <Schema xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices"
        xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata"
        xmlns="http://schemas.microsoft.com/ado/2008/09/edm" Namespace="ODataWeb.Northwind.Model">
        <EntityContainer
            xmlns:p7="http://schemas.microsoft.com/ado/2009/02/edm/annotation"
            Name="NorthwindEntities" p7:LazyLoadingEnabled="true"
            m:IsDefaultEntityContainer="true">
            <EntitySet Name="Employees" EntityType="NorthwindModel.Employee" />
            <EntitySet Name="Appointments" EntityType="NorthwindModel.Appointment"/>
            <AssociationSet Name="FK_Employees_Appointments" Association="NorthwindModel.FK_Employees_Appointment">
                <End Role="Employees" EntitySet="Employees"/>
                <End Role="Appointments" EntitySet="Appointments"/>
            </AssociationSet>
        </EntityContainer>
    </Schema>
</edmx:DataServices>

(对于标签AssociationSet和NavigationProperty,我这样做是因为我在我使用的OData服务上看到过,但我不明白;我猜它会没事的......)

现在是 Appointments.json 文件:

[
{
    "AppointmentID" : 0,
    "EmployeeID" : 0,
    "start":"\/Date(1466416800000)\/",
    "end":"\/Date(1466424000000)\/",
    "title": "test"
},{
    "AppointmentID" : 1,
    "EmployeeID" : 1,
    "start":"\/Date(1466409600000)\/",
    "end":"\/Date(1466416800000)\/",
    "title": "test2"
}]

Employees.json 文件:

[
{
    "EmployeeID":0,
    "LastName":"APERCE",
    "City":"Paris",
    "Team":"Dev"
},
{
    "EmployeeID":1,
    "LastName":"HACHMI",
    "City":"Lille",
    "Team":"Dev"
},
{
    "EmployeeID":2,
    "LastName":"MILLET",
    "City":"Paris",
    "Team":"Admin"
},
{
    "EmployeeID":3,
    "LastName":"CORNET",
    "City":"Poitiers",
    "Team":"Admin"
},
{
    "EmployeeID":4,
    "LastName":"EVAIN",
    "City":"Paris",
    "Team":"R&D"
}]

(如您所见,EmployeeID 存在于 Appointments.json 和Employees.json 中)

最后是 Overview.view.xml 文件:

<mvc:View controllerName="sap.ui.demo.wt.controller.Overview"
xmlns="sap.m" xmlns:mvc="sap.ui.core.mvc" xmlns:semantic="sap.m.semantic"
xmlns:unified="sap.ui.unified"
xmlns:core="sap.ui.core" displayBlock="true">
<semantic:FullscreenPage title="{i18n>overviewPageTitle}">
    <VBox>
        <PlanningCalendar startDate="{path : '/Startdate'}" rows="{path : 'data>/Employees',parameters:{expand:'Appointments'}}"
            appointmentSelect="handleAppointmentSelect">
            <rows>
                <PlanningCalendarRow title="{data>LastName}"
                    appointments="{path : 'data>Appointments',templateShareable:true}">
                    <appointments>
                        <unified:CalendarAppointment
                            startDate="{data>start}"
                            endDate="{data>end}"
                            title="{data>title}">

                        </unified:CalendarAppointment>
                    </appointments>
                </PlanningCalendarRow>
            </rows>
        </PlanningCalendar>
    </VBox>
</semantic:FullscreenPage>

(“数据”模型已经与 metadata.xml 绑定并同时获取 Appointments.json 和Employees.json)

实际上,此代码显示 Appointments.json 中针对我计划中的每个员工的两个任务。我想绑定它以便只为员工“APERCE”预约“test”,只为“HACHMI”预约“test2”,但我不能。

我相信可以为 SQL 数据库执行与 JOIN 等效的操作,但我没有找到任何相关信息。我应该怎么做?谢谢你的回答。

PS:我是法国人,如果你不明白我上面写的内容,对不起;如果你能用法语回答,那就这样做,这对我的理解会更好。

编辑:我已根据 nistv4n 的建议更正了代码。现在我收到以下错误:

MockServer: Resource not found for the segment 'Appointment' => 我知道,'Appointment' 部分不存在,因为它是 'Appointments',但我不知道在哪里我忘记了's'。

出现以下问题:HTTP request failed404,Not Found,{"error":{"code":404,"message":{"lang":"en","value":"Resource not found for the segment 'Appointment'"}}} => 我猜这与上一个错误有关,因为它请求的是 'Appointment' 而不是 'Appointments' ?

重新编辑:我把“s”放在正确的位置。现在我只有:无法读取未定义的属性'getTime' =>,因为Overview.view.xml中使用的模型可能存在问题,但如果我使用它也是一样的:“{Appointments/start}” 、“{Appointments>start}”、“{start}”或“{/Appointments>start}”。

LASTEDIT : 多亏了 nistv4n,我终于做到了。如果您有同样的问题,我已经更新了上面的代码。正确的代码在 PlanningCalendarRow => appointments="{path : 'data>Appointments'}" 和统一中:CalendarAppointment => startDate="{data>start}" 等等……

实际上,引用 nistv4n :“约会有相对引用,内部属性(开始、结束、标题)也有相对引用,包括模型名称。”

【问题讨论】:

  • startDate="{data>start}", endDate="{data>end}", title="{data>title}" 怎么样。
  • 每个员工都有任务“test”和“test2”。但我只希望“APERCE”的“测试”和“HACHMI”的“test2”,而不是其他员工。
  • @Ash 实际上你是对的,问题出在 PlanningCalendarRow 中。谢谢!

标签: javascript json xml metadata sapui5


【解决方案1】:

在为PlanningCalendar 的聚合行定义绑定的地方,包括$expand 关键字以使引用的活动在容器中可访问:

<PlanningCalendar startDate="{path : '/Startdate'}" rows="{path : 'data>/Employees', parameters:{expand:'Appointment'}}" 
appointmentSelect="handleAppointmentSelect">

它将为给定的员工获取分配的Appointment 数据。您可以使用聚合中的Appointment/start 绑定路径来引用。

编辑: 您似乎错过了第一个模式中的关联节点。像这样定义它:

<Association Name="FK_Employees_Appointment">
    <End Type="Employee" Role="Employees" Multiplicity="1" />
    <End Type="Appointment" Role="Appointments" Multiplicity="*" />
    <ReferentialConstraint>
        <Principal Role="Employees">
            <PropertyRef Name="EmployeeID" />
        </Principal>
        <Principal Role="Appointments">
            <PropertyRef Name="EmployeeID" />
        </Principal>
    </ReferentialConstraint>
</Association>

我建议只定义两个实体之间的单向连接。

您能否将您的代码发布到某个地方(即使是在 zip 存档中)以研究整个架构和代码线?

编辑 2:请根据以下内容更改您的 View XML 的 PlanningCalendarRow 段:

<PlanningCalendarRow title="{data>LastName}"
    appointments="{path : 'data>Appointments',templateShareable:true}">
    <appointments>
        <unified:CalendarAppointment
            startDate="{data>start}"
            endDate="{data>end}"
            title="{data>title}">

        </unified:CalendarAppointment>
    </appointments>
</PlanningCalendarRow>

Appointments 有一个相对引用,内部属性(startendtitle)也有一个相对引用,包括模型名称。

【讨论】:

  • 好的,只是约会带有's',否则它不会识别。现在我有以下错误: 1) MockServer: Resource not found for the segment 'Appointments' |||| 2)出现如下问题:HTTP request failed404,Not Found,{"error":{"code":404,"message":{"lang":"en","value":"resource not found for the segment '约会'"}}} 我想我错过了 metadata.xml 中的某些内容,例如第一个 中的 标记。
    我的同事让我阅读 odata.org 上的文档,我会看这边,但它大约有 350 页。
  • 如果我写 Appointment w/o an 's' 我得到这个错误:出现以下问题:HTTP request failedundefined,undefined,HTTP/1.1 NaN Content-Type: application/json Content-Length : 0 数据服务版本: 2.0
  • @NicolasApercé 我刚刚意识到我在关联定义中留下了一些旧代码 - End Type="" 部分应该与您的架构对齐,我更新了我的答案。我还希望将整个项目视为 ZIP 或 JSBin,因为动态调试模拟服务器非常困难。
  • 我怎样才能将它作为 ZIP 发送给您?我不知道如何使用 JSBin
  • @NicolasApercé 如果您使用 WebIDE,只需下载该项目,或者从 Eclipse 中导出,或者压缩整个文件夹。之后,您可以将其上传到互联网以使其可供下载。
猜你喜欢
  • 2018-05-13
  • 1970-01-01
  • 2023-03-11
  • 1970-01-01
  • 1970-01-01
  • 2022-10-24
  • 2013-02-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多