dre75230 2016-01-08 14:14
浏览 39
已采纳

apigility设置日期字段的默认值

I try to create the simple api using apigility to save some data. I have the table structure as below. Using apigility admin ui, I created the Resetful post service for below entity. createdAt field is optional but I want make sure the field should filed by current datetime when it's blank in request. Can someone give procedure to do it.

id integer,
name string,
createdAt datetime,
updatedAt timestamp,

Here is how admin field setup looks.

I also like to know how to set the validators to allow user to enter the createdAt as "Y-m-d H:i:s" format. can you guide me please enter image description here

  • 写回答

1条回答 默认 最新

  • duan0821 2016-01-12 07:52
    关注

    There are a number of ways you could go about doing this:

    1) Make the default to some date or date function in your persistence layer (as in make it default to now() or something in your database).

    2) In your resource's create, update, patch, etc, check if the field has been passed in. If not, create a default timestamp to inject. You could also do it further down in your dependency layers, like in your service, mapper, or other resource dependency, where ever it makes sense.

    3) Create something like a NullToDefaultDate filter. It will run on your null field and change the null to the default date you provide before it gets into your apigility code. Be sure you're either getting the filtered values from the injected InputFilter through $this->getInputFilter()->getValues(); or similar or you have set up use_raw_data = false in your module.config.php in the zf-content-validation section.

    'zf-content-validation' => array( 'YourNamespace\\V1\\Rest\\YourServiceName\\Controller' => array( 'input_filter' => 'YourNamespace\\V1\\Rest\\YourServiceName\\Validator', 'use_raw_data' => false, 'allows_only_fields_in_filter' => true, ) )

    If you put in just 'use_raw_data' => false, you'll get the filtered values for the fields you've defined in Apigility, but you'll also get unfiltered additional "garbage" fields that you've not told Apigility about at all. If you don't want those, you can enable allows_only_fields_in_filter which will only give you back fields you've defined, but now your API return an ApiProblem error when a field that was not in the list is passed in. Using $data = $this->getInputFilter()->getValues() in your methods will give you back just the fields you've defined and it will give you the filtered values as well.

    For the second part of your question, you want to use the Zend\Validator\Date validator and add a format option of 'Y-m-d H:i:s'.

    Apigility Admin Format for Date Validator

    Be sure to click Add Option before save or it won't keep it.

    Hope this helps.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算