将日期数组保存到数据库

发布于 2024-10-04 21:34:44 字数 506 浏览 4 评论 0原文

所以,我的表单中有这个元素:

<?php
$form['order']['date'] = array(
        '#type'          => 'date',
        '#title'         => 'Order date',
      );

在表单提交函数中,我有这样的数组:

[values] => Array
    (
        [date] => Array
            (
                [year] => 2010
                [month] => 11
                [day] => 27
            )
    )

我正在寻找一种简单的方法来将该数组保存在数据库中,然后我希望能够从数据库中提取该日期将被替换回编辑表单。

drupal api 函数有吗?

So, I have this element in my form:

<?php
$form['order']['date'] = array(
        '#type'          => 'date',
        '#title'         => 'Order date',
      );

In the form submit function I have got such array:

[values] => Array
    (
        [date] => Array
            (
                [year] => 2010
                [month] => 11
                [day] => 27
            )
    )

I am looking for a simple way to save this array in database and then I want to be able to extract this date from the database for that would be substituted back into the edit form.

Are there drupal api functions for this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

套路撩心 2024-10-11 21:34:45

我认为 Date 模块会给你 API 调用来做到这一点。

就我而言,我的要求相当简单,并且我不想创建额外的模块依赖项,因此我使用以下内容来形成 PHP DateTime 对象:

$date_arr = $form_state['values']['date'];
if ($date_arr['year'] && $date_arr['month'] && $date_arr['day']) {
  $date = new DateTime();
  $date->setDate($date_arr['year'], $date_arr['month'], $date_arr['day']);
} else {
  $date = NULL;
}

存储在数据库中(作为 UNIX 时间戳,按照 Drupal 约定) ,我使用了$date->getTimestamp()。

我不需要将日期放回到表单中,但如果需要,我会尝试类似的操作:

$query = db_query("select mydate from table");
while ($fields = db_fetch_array($query)) {
  $date = new DateTime();
  $date->setTimestamp($fields[0]);
  $form['order']['date'] = array(
    '#type'          => 'date',
    '#title'         => 'Order date',
    '#default_value' => array(
      year  => $date->format('Y'),
      month => $date->format('m'),
      day   => $date->format('d'),
    ),
  );
}

我还没有测试过这个,所以你需要进行实验。

希望这有帮助!

I think the Date module will give you API calls to do this.

In my case my requirements were fairly simple, and I didn't want to create an extra module dependency so I used the following to form a PHP DateTime object:

$date_arr = $form_state['values']['date'];
if ($date_arr['year'] && $date_arr['month'] && $date_arr['day']) {
  $date = new DateTime();
  $date->setDate($date_arr['year'], $date_arr['month'], $date_arr['day']);
} else {
  $date = NULL;
}

To store in a database (as a UNIX timestamp, as is the Drupal convention), I used $date->getTimestamp().

I didn't need put date back into a form, but if I did, I'd try something like:

$query = db_query("select mydate from table");
while ($fields = db_fetch_array($query)) {
  $date = new DateTime();
  $date->setTimestamp($fields[0]);
  $form['order']['date'] = array(
    '#type'          => 'date',
    '#title'         => 'Order date',
    '#default_value' => array(
      year  => $date->format('Y'),
      month => $date->format('m'),
      day   => $date->format('d'),
    ),
  );
}

I haven't tested this, so you'll need to experiment.

Hope this helps!

鱼忆七猫命九 2024-10-11 21:34:45

您可以序列化它并将其作为序列化数组存储在数据库中。然后,当您想要显示它时,您可以在数据库列上调用 unserialize ,然后您就可以恢复原始数组了。

//in your submit function
$date = serialize($values['date']);
$sql = "UPDATE foobar SET date = '%s' WHERE id = '%d'";
db_query($sql, $date, $some_id);

//in your form function
$date_array = db_result(db_query("SELECT date FROM foo WHERE id = '%d'", $some_id));
$form['date'] = array(
  '#type' => 'date',
  '#title' => 'Order date',
  '#default_value' => $date_array,  
);

You could just serialize it and store it as a serialized array in the database. Then when you want to display it, you call unserialize on the database column and you have the original array back.

//in your submit function
$date = serialize($values['date']);
$sql = "UPDATE foobar SET date = '%s' WHERE id = '%d'";
db_query($sql, $date, $some_id);

//in your form function
$date_array = db_result(db_query("SELECT date FROM foo WHERE id = '%d'", $some_id));
$form['date'] = array(
  '#type' => 'date',
  '#title' => 'Order date',
  '#default_value' => $date_array,  
);
_畞蕅 2024-10-11 21:34:45

首先,这取决于您需要如何存储日期。时间戳、日期、日期时间或某种自定义格式?

您不会找到任何 API 将日期转换为日期字符串,但如果您使用日期模块创建日期字段,日期模块会将数据转换为数据库使用的常用 ISO 格式的日期字符串。

First of all, it depends on how you need to store the date. A time stamp or a date or a datetime or some custom format?

You wont find any API to make the date into a date string, but if you use the date module to create a date field, the date module will convert the data to a date string with the usual ISO format that databases use.

絕版丫頭 2024-10-11 21:34:45

我可以为你指出正确的方向。

API 中的功能可以为您完成这项工作。日期有点繁琐,需要一些耐心。

查看表单参考 API 中的日期元素。可能有一个钩子可以用来进行格式化。

http://api.drupal.org/api/drupal /developer--topics--forms_api_reference.html/6

您应该熟悉 hook_elements()。有一些您会感兴趣的属性,例如“#process”,您可以在其中添加回调到在表单提交时执行的日期元素。这个钩子的评价非常低。

http://api.drupal.org /api/drupal/developer--hooks--core.php/function/hook_elements/6

配置文件模块有一个示例(虽然很难找到):

http://api.drupal.org/api/drupal/modules--profile--profile .module/function/profile_form_profile/6

日期主要作为时间戳存储在数据库中。这些可以使用 format_date 轻松转换:

http:// api.drupal.org/api/drupal/includes--common.inc/function/format_date/6

:)

I can point you in the right direction.

There is functionality in the API to do the work for you. Dates are a bit fiddly and will require some patience.

Take a look at the date element in the form reference API. There's probably a hook that you can use to do your formatting.

http://api.drupal.org/api/drupal/developer--topics--forms_api_reference.html/6

You should be familiar with hook_elements(). There's some attributes that will interest you such as '#process' where you can add callbacks to the date element that get executed on form submit. This hook is very under rated.

http://api.drupal.org/api/drupal/developer--hooks--core.php/function/hook_elements/6

The profile module has an example (hard to find it though):

http://api.drupal.org/api/drupal/modules--profile--profile.module/function/profile_form_profile/6

Dates are mostly stored in the database as timestamps. These can be easily converted with format_date:

http://api.drupal.org/api/drupal/includes--common.inc/function/format_date/6

:)

花之痕靓丽 2024-10-11 21:34:45

Drupal 对数据库中的日期数组使用(取消)序列化,我假设它是一个配置文件字段。如果你使用serialize将其写入数据库,Drupal就可以使用它。

Drupal uses (un)serialize for the date array in the db, i assume it's a profile field. If you use serialize to write it to the db, Drupal can use it.

雪若未夕 2024-10-11 21:34:45

Drupal 中日期存储的首选方法是 UNIX 时间格式。我建议使用 mktime 函数将表单中的日期转换为 UNIX 时间 或类似的东西。

这是一个简单的示例:

$my_date = mktime(23, 59, 59, $order->values['date']['month'], $order->values['date']['day'], $order->values['date']['year']);

然后在数据库插入/更新中使用 $my_date 变量。

The preferred method of date storage in Drupal is in UNIX time format. I would recommend converting the date in your form to UNIX time using the mktime function or something similar.

Here's a simple example:

$my_date = mktime(23, 59, 59, $order->values['date']['month'], $order->values['date']['day'], $order->values['date']['year']);

Then use the $my_date variable in your DB insert/update.

静水深流 2024-10-11 21:34:45

我想我找到了一个简单的方法来做到这一点,但它不使用 Drupal api。

$date = implode($form_state['values']['date']); // $date = 20101127;

I think I found a simple way to do it, but it does not use Drupal api.

$date = implode($form_state['values']['date']); // $date = 20101127;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文