SAPUI5 Metadata.xml : 绑定两个实体

SAPUI5 Metadata.xml : binding two entities

我在一家公司实习,我必须使用 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"
}]

(如您所见,Appointments.json 和 Employees.json 上都存在 EmployeeID)

最后 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>

("data" 模型已经与 metadata.xml 绑定并获得 Appointments.json 和 Employees.json)

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

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

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

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

MockServer:未找到段 'Appointment' 的资源 => 我知道,段 'Appointment' 不存在,因为它是 'Appointments',但我不知道它在哪里我忘了 's'.

发生以下问题:HTTP 请求失败404,未找到,{"error":{"code":404,"message":{"lang":"en","value":"Resource not found for the segment 'Appointment'"}}} => 我想这与之前的错误有关,因为它请求 'Appointment' 而不是 'Appointments' ?

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

LASTEDIT : 感谢 nistv4n,我终于做到了。如果您有同样的问题,我已经更新了上面的代码。正确的代码在 PlanningCalendarRow => appointments="{path : 'data>Appointments'}" 和 unified:CalendarAppointment => startDate="{data >开始}" 等...

实际上,引用 nistv4n:"Appointments has a relative reference, and inner properties (start, end, title) has also a relative reference, including the model name."

在为 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>

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

你能post你的代码到某个地方(即使在 zip 存档中)来调查整个架构和代码行吗?

编辑 2:请根据此更改视图 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)也有相对引用,包括模型名称。