简体   繁体   English

Joomla,JForm:日期和时间未保存

[英]Joomla, JForm: Date and time aren't being saved

New at Joomla dev, writing a simple backend component where I have two date fields. Joomla开发人员的新功能,它编写了一个简单的后端组件,其中有两个日期字段。 One is a date the user can choose from a calendar popup (1st one below), the second is a date/time stamp that the user can't change but needs to see what that last timestamp was. 第一个是用户可以从日历弹出窗口中选择的日期(下面的第一个),第二个是用户不能更改但需要查看最后一个时间戳是什么的日期/时间戳。 In the edit form the first one (datepassed) is working and grabbing today's date. 在编辑表单中,第一个(日期已过)正在工作并获取今天的日期。 The second (timestamp) only works when the record is created and the time isn't being saved at all. 第二个(时间戳)仅在创建记录且根本不保存时间时才起作用。 I need it to always grab the present time and date. 我需要它来始终掌握当前时间和日期。

From the XML file: 从XML文件:

  <field name="datepassed"
    type="calendar"
    label="Date Completed"
    default="NOW"           
  /> 

  <field name="timestamp"
    type="calendar"
    label="Last Modified"
    default="NOW"
    readonly="true" 
    class="readonly"
  />

And in the edit.php view: 并在edit.php视图中:

  foreach ($this->form->getFieldset('fields_logged') as $field): 
    echo '<li>';
        echo $field->label;
        echo $field->input; 
     echo '</li>';
  endforeach;

Found this link with a similar question, and this one from Joomla docs ...not much help in the way of time though. 发现了与此类似的问题的链接 ,而Joomla文档中的这个问题……在时间上并没有太大帮助。

Any ideas why that timestamp isn't working, or is there a better way of doing this? 有什么想法为什么时间戳不起作用,或者有更好的方法吗?

Thanks! 谢谢!

There is a more simple way of doing what you want, no need to write override for model, we its another attribute for the calendar type field in joomla, refer http://docs.joomla.org/Calendar_form_field_type 有一种更简单的方法可以执行所需的操作,无需为模型编写覆盖,我们在joomla中将其作为日历类型字段的另一个属性,请参阅http://docs.joomla.org/Calendar_form_field_type

you can use in your code like, 您可以在代码中使用

 <field name="datepassed"
type="calendar"
label="Date Completed"
default="NOW"    
 format="%d-%m-%Y H:i:s"
 /> 

<field name="timestamp"
type="calendar"
label="Last Modified"
default="NOW"
readonly="true" 
class="readonly"
format="%d-%m-%Y H:i:s"
 />

OK, from your updated question, it appears there are two simple misunderstandings going on. 好的,从您更新的问题来看,似乎存在两个简单的误解。

The default value is only set if the field value is null so it will work on the first save but subsequently as there is a value it won't be replaced with the current timestamp. 仅当该字段值为null时才设置default值,这样它将在第一次保存时起作用,但是随后由于有一个值,它将不会被当前时间戳替换。

When you use a type of Calendar it will only return the date sans the time component. 当您使用Calendar类型时,它将仅返回日期(不含时间部分)。 If you look at the /libraries/joomla/html/parameter/element/calendar.php you will see that it's asking for '%Y-%m-%d' 如果您查看/libraries/joomla/html/parameter/element/calendar.php您会发现它要求的是'%Y-%m-%d'

To update the timestamp you will want to modify your model so that when you get the item it updates the timestamp field with the current date & time eg $item->timestamp = JFactory::getDate() 要更新时间戳,您将需要修改模型,以便在获取项目timestamp使用当前日期和时间更新timestamp字段,例如$item->timestamp = JFactory::getDate()

Or possibly update it just in the store() if you need it to be minutes and seconds accurate as conceivable the item could be opened and not saved for several minutes. 或者,如果您需要精确到几分钟和几秒钟,则可以只在store()更新,因为可以想到该项目可能会被打开而不在几分钟内保存。 This is probably the better approach as you want your user to be able to see the last time the item was saved. 这可能是更好的方法,因为您希望用户能够看到上次保存项目的时间。

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

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