简体   繁体   English

将空值传递给 OData V2 Edm.Time 属性

[英]Pass an empty value to an OData V2 Edm.Time property

I have a variable type time but sometimes this variable doesn't have anything.我有一个变量类型时间,但有时这个变量没有任何东西。

When it is initial, it shouldn't be "000000" , I want an empty value without anything (no zeros).当它是初始时,它不应该是"000000" ,我想要一个没有任何东西的空值(没有零)。 Let me explain my problem with the code:让我用代码解释我的问题:

IF lwa_hora IS INITIAL.
   CLEAR lwa_hora.
   ls_entity-hora = lwa_hora. " Result: 000000 but I don't want any zero
ELSE.
   ls_entity-hora = lwa_hora. " Result: 000000
ENDIF.

I tried with CLEAR but nothing happens.我尝试使用CLEAR但没有任何反应。

I need this is because in the JavaScript frontend client logic, I need the OData property to contain a falsy value (Eg null or an empty string "" ).我需要这是因为在 JavaScript 前端客户端逻辑中,我需要 OData 属性包含一个假值(例如null或空字符串"" )。

But it always has the value "000000" which is not an empty string.但它始终具有值"000000" ,这不是空字符串。 Is it possible to make something in the backend to "clear" the variable?是否可以在后端做一些事情来“清除”变量?

The time data-type in abap ( t ) is a value-type. abap ( t ) 中的时间数据类型是值类型。 It's internally implemented as an integer counting the seconds since midnight.它在内部实现为一个整数,从午夜开始计算秒数。 0 seconds since midnight is a valid value, so it can't have a null-value.从午夜开始的 0 秒是一个有效值,因此它不能有空值。

However, ABAP allows you to create a reference to any value-type:但是,ABAP 允许您创建对任何值类型的引用:

hora TYPE REF TO t.

That means hora will be a reference to a variable of TYPE t .这意味着hora将是对TYPE t变量的引用。 Initially, this reference will be unbound, which is conceptually very similar to a null-reference in other programming languages.最初,此引用将是未绑定的,这在概念上与其他编程语言中的空引用非常相似。 You can check that with:您可以通过以下方式检查:

IF ls_entity-hora IS BOUND.
...
IF ls_entity-hora IS NOT BOUND.

You can assign a time value with GET REFERENCE OF lwa_hora INTO ls_entity-hora .您可以使用GET REFERENCE OF lwa_hora INTO ls_entity-hora分配时间值。 But now you have a reference to an existing variable.但是现在您有了对现有变量的引用。 Change the value of lwa_hora , and the value of ls_entity-hora also changes.改变lwa_hora的值, ls_entity-hora的值也会改变。 That might not always be what you want.这可能并不总是你想要的。 So it might be better to create a new piece of data in memory for our reference to point to:因此,最好在内存中创建一个新数据供我们参考以指向:

CREATE DATA ls_entity-hora.

Now ls_entity-hora is no longer unbound (or "null" if you want to call it that), it points to a new time-value of 000000 .现在ls_entity-hora不再是未绑定的(或者如果你想称之为“null”),它指向一个新的时间值000000 If you want to read or change the value of this nameless piece of data this reference points to, then you can do this with the dereferencing-operator ->* :如果要读取或更改此引用指向的无名数据的值,则可以使用 dereferencing-operator ->*执行此操作:

ls_entity-hora->* = lwa_hora.

If you intentionally want to set a reference to unbound (or "set the reference to null"), you can do that by clearing the reference:如果您有意将引用设置为未绑定(或“将引用设置为 null”),您可以通过清除引用来实现:

CLEAR ls_entity-hora.

By the way: Representing a point in time by two separate variables of the types d and t fell out of fashion in the past decade.顺便说一句:在过去十年中,用dt类型的两个独立变量来表示时间点已经过时了。 The current best practice for this situation is to use a single variable of type TIMESTAMP (if you need second precision) or TIMESTAMPL (if you need microsecond precision).当前针对这种情况的最佳实践是使用TIMESTAMP (如果需要第二精度)或TIMESTAMPL (如果需要微秒精度)类型的单个变量。 A timestamp of 00000000000000 is obviously an illegal value, so it can be used to represent the absence of a point in time.时间戳00000000000000显然是非法值,因此可以用来表示时间点的缺失。 This type also usually makes it much easier to communicate with a SAPUI5 frontend (like in your case), because many of the technologies for making oData services offer automatic conversion between Javascript Date and ABAP TIMESTAMP .这种类型通常还可以更轻松地与 SAPUI5 前端(如您的情况)进行通信,因为许多用于制作 oData 服务的技术都提供了 Javascript Date和 ABAP TIMESTAMP之间的自动转换。

An alternative to heap allocating the time would be to store a boolean next to it, indicating whether it is set or not:堆分配时间的另一种方法是在它旁边存储一个布尔值,指示它是否已设置:

TYPES:
     BEGIN OF optional_time,
        time    TYPE t,
        is_null TYPE abap_bool,
     END OF optional_time.

DATA(no_time) = VALUE optional_time( is_null = abap_true ).

" Setting to null:
DATA(some_time) = no_time.
" Setting to some time:
some_time = VALUE #( time = '12:30' ).

IF some_time = no_time.
   " ...
ENDIF.

This sort of things is probably better to handle on front-end than on back-end.这类事情在前端处理可能比在后端处理更好。 SAP Gateway serialized ABAP Date/time initial values to NULL if the OData response if the corresponding property is nullable.如果 OData 响应对应的属性可以为空,则 SAP Gateway 将 ABAP 日期/时间初始值序列化为 NULL。 You need to make sure this property is set to TRUE like in this sample您需要确保在此示例中将此属性设置为TRUE

在此处输入图片说明

you can also set this property in runtime您还可以在运行时设置此属性

TRY .
 lo_action = model->get_entity_type( iv_entity_name = 'Z_REPORTType').                                                             
 lo_property = lo_action->get_property( iv_property_name = 'Requested_Date').
 lo_property->set_nullable( iv_nullable = abap_true ).
CATCH /iwbep/cx_mgw_busi_exception /iwbep/cx_mgw_med_exception /iwbep/cx_mgw_tech_exception INTO DATA(lo_exception).
ENDTRY.

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

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