简体   繁体   English

SAPUI5 Metadata.xml:绑定两个实体

[英]SAPUI5 Metadata.xml : binding two entities

I am in internship in a firm where I have to code in JS using SAPUI5 framework. 我在一家公司实习,我必须使用SAPUI5框架在JS中编码。 It's totally new for me, that's why I followed the tutorial at http://sapui5.hana.ondemand.com . 这对我来说是全新的,这就是我遵循http://sapui5.hana.ondemand.com上的教程的原因。 But now I have a problem, I can't find any advise on the Internet, and I can't always ask my colleagues to help me, they have a job too... 但是现在我遇到了问题,我在互联网上找不到任何建议,而且我不能总是要求我的同事帮助我,他们也有工作......

I have to do a planning with many employees and their task. 我必须与许多员工及其任务进行规划。 I work with a mockserver with a metadata.xml file : 我使用带有metadata.xml文件的mockserver:

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

(for tags AssociationSet and NavigationProperty, I did it because I've seen on the OData service I use, but I don't understand it; I guessed it will be fine...) (对于标签AssociationSet和NavigationProperty,我这样做是因为我在我使用的OData服务上看到过,但我不明白它;我猜它会好的......)

Now the Appointments.json file : 现在是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"
}]

The Employees.json file : 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"
}]

(As you can see, the EmployeeID is present on both Appointments.json et Employees.json) (如您所见,EmployeeID出现在Appointments.json和Employees.json上)

And finally the Overview.view.xml file : 最后是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>

(the "data" model binds already with the metadata.xml and gets both Appointments.json and Employees.json) (“data”模型已经与metadata.xml绑定并获取Appointments.json和Employees.json)

Actually, this code displays the two tasks from Appointments.json for every employee in my planning. 实际上,此代码在我的计划中为每个员工显示Appointments.json中的两个任务。 I want to bind it in order to have the appointment "test" only for the employee "APERCE" and "test2" only for "HACHMI", but I can't. 我想绑定它,以便仅为“HACHMI”的员工“APERCE”和“test2”进行约会“测试”,但我不能。

I believe it is possible to do something equivalent to a JOIN for a SQL Database, but I don't find anything about it. 我相信可以为SQL数据库执行等同于JOIN的操作,但我找不到任何关于它的信息。 How should I do it ? 我该怎么办? Thank you for your answers. 谢谢您的回答。

PS : I am french, if you don't understand what I wrote above, sorry ; PS:我是法国人,如果你不明白我上面写的,对不起; and if you can answer in french, do so, it will be better for my understanding. 如果你能用法语回答,那么这对我的理解会更好。

EDIT : I have corrected the code according to nistv4n's advises. 编辑:我已根据nistv4n的建议更正了代码。 Now I get the following errors : 现在我收到以下错误:

MockServer: Resource not found for the segment 'Appointment' => I understand that, the segment 'Appointment' doesn't exist since it's 'Appointment s ', but I don't know where I forgot the 's'. MockServer:资源未找到段“约会” =>我的理解是,该段“约会”不存在,因为它是“约会S”,但我不知道在哪里,我忘了“S”。

The following problem occurred: HTTP request failed404,Not Found,{"error":{"code":404,"message":{"lang":"en","value":"Resource not found for the segment 'Appointment'"}}} => I guess it's related to the previous error, because it requests for 'Appointment' instead of 'Appointment s ' ? 发生以下问题:HTTP请求失败404,未找到,{“错误”:{“code”:404,“message”:{“lang”:“en”,“value”:“找不到该段的资源'预约““}}} =>我想这是与以前的错误,因为它要求对‘约会’而不是‘预约s’吗?

REEDIT : I put the 's' on the right places. REEDIT:我把's'放在正确的位置。 Now I just have : Cannot read property 'getTime' of undefined => because there is a problem with the model used in Overview.view.xml, probably in the , but it's the same if I use : "{Appointments/start}", "{Appointments>start}", "{start}" or "{/Appointments>start}". 现在我只是:无法读取undefined =>的属性'getTime',因为在Overview.view.xml中使用的模型存在问题,可能在,但是如果我使用它是相同的:“{Appointments / start}” ,“{约会>开始}”,“{开始}”或“{/约会>开始}”。

LASTEDIT : Thanks to nistv4n, I finally did it. LASTEDIT:感谢nistv4n,我终于做到了。 I have updated my code above if you have the same problem. 如果您遇到同样的问题,我已经更新了上面的代码。 The correct code is in PlanningCalendarRow => appointments="{path : 'data>Appointments'}" and in unified:CalendarAppointment => startDate="{data>start}" etc... 正确的代码在PlanningCalendarRow => appointmentments =“{path:'data> Appointments'}”并统一:CalendarAppointment => startDate =“{data> start}”等...

Actually, to quote nistv4n : "Appointments has a relative reference, and inner properties (start, end, title) has also a relative reference, including the model name." 实际上,引用nistv4n:“约会有一个相对引用,内部属性(开始,结束,标题)也有一个相对引用,包括模型名称。”

Where you define the binding for aggregation rows of PlanningCalendar , include the $expand keyword to make the referenced activities accessible in the container: 在为PlanningCalendar聚合行定义绑定的位置,包含$expand关键字以使容器中的引用活动可访问:

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

It will fetch the assigned Appointment data for the given employee. 它将获取给定员工的指定Appointment数据。 You can refer to that using the Appointment/start binding path inside the aggregation. 您可以使用聚合内的Appointment/start绑定路径来引用它。

Edit: It seems you missed the Association node from the first schema. 编辑:您似乎错过了第一个架构的关联节点。 Define it like this: 像这样定义:

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

I would recommend to define only one-way connection between the two entities. 我建议只定义两个实体之间的单向连接。

Could you post your code to somewhere (even in a zip archive) to investigate the whole architecture and codeline? 您是否可以将代码发布到某个地方(甚至在zip存档中)来调查整个架构和代码行?

Edit 2: Please change the PlanningCalendarRow segment of your View XML according to this: 编辑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 has a relative reference, and inner properties ( start , end , title ) has also a relative reference, including the model name. Appointments具有相对引用,内部属性( startendtitle )也具有相对引用,包括模型名称。

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

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