【问题标题】:updating timestamp field in mysql using joomla component使用joomla组件更新mysql中的时间戳字段
【发布时间】:2012-12-10 20:38:26
【问题描述】:

我正在开发 joomla 组件 (com_book) 3.0 版。我正在尝试使用 form 将书籍插入到 database,在数据库中我有 updated_at 列,我正在使用 timestamp datatype。当我使用数据库插入和更新书籍并使用表单插入书籍时,updated_at 列工作正常,那么它工作正常,但是当我尝试使用表单更新书籍时,update_at 列没有更新。

谁能解释我在这个过程中犯了什么错误?

【问题讨论】:

  • 请提供您的更新声明?
  • updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP;这个语句我用于该 updated_at 列。
  • 您能否将您的查询放在问题中以便我们弄清楚

标签: joomla joomla2.5 joomla-extensions


【解决方案1】:

另一种方法是为此创建一个自定义字段,

在您的 book.xml 表单文件中使用此域代码

<fieldset
addfieldpath="/administrator/components/com_book/models/fields"
>
..
..
<field name="timestamp" type="lastmodified"
label="" description="" />
..
..
</fieldset>

并创建一个名为 lastmodified.php 的文件 在您的 /administrator/components/com_book/models/fields 文件夹中

<?php

defined('JPATH_BASE') or die;

jimport('joomla.form.formfield');

/**
 * Supports an HTML form field
 */
class JFormFieldLastModified extends JFormField
{
    /**
     * The form field type.
     *
     * @var        string
     * @since    1.6
     */
    protected $type = 'lastmodified';

    /**
     * Method to get the field input lastmodified.
     *
     */
    protected function getInput()
    {
        // Initialize variables.
        $html = array();
        $old_time_updated = $this->value;
        if ($old_time_updated) {
            $jdate = new JDate($old_time_updated);
            $pretty_date = $jdate->format(JText::_('DATE_FORMAT_LC2'));
        }
        $time_updated = date("Y-m-d H:i:s");
        $html = '<input type="hidden" name="'.$this->name.'" value="'.$time_updated.'" />';

        return $html;
    }
}

?>

【讨论】:

    【解决方案2】:

    Joomla 组件创建器可以构建 Joomla 3.0 组件,让您免于处理此类简单的事情。

    看一看:http://www.notwebdesign.com/joomla-component-creator/

    一张桌子免费。如果没有别的,您可以从那里复制粘贴代码或峰值,看看如何开发自己。

    【讨论】:

      【解决方案3】:

      在数据绑定后的保存功能中可以设置updated_at

      // Bind the data to the table
      if (!$table->bind($post))
      {
      //display error
      }
      //after binding write this line-
      $table->updated_at  = date('Y-m-d H:i:s');
      

      如果这不起作用,请告诉我。

      【讨论】:

      • 您好 Irfan,我使用的是 joomla 3.0 版。在该组件中无需编写保存功能。所以我的组件中没有保存功能。
      【解决方案4】:

      将此函数粘贴到表格文件中,例如您的表格 Joomla根/管理员/com_book/table/book.php 如果已经有一个存储功能,那么编辑它并单独添加这些行,这样就可以完成这项工作,

          public function store($updateNulls = false)
      {
          $date   = JFactory::getDate();
                     // this is your primary key maybe id or book_id
          if ($this->book_id) {
              // Assigning the last modified date to your timestamp field
              $this->timestamp    = $date->toSql();
          }
      
          // Attempt to store the user data. - just leave the rest to parent function
          return parent::store($updateNulls);
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-08-01
        • 1970-01-01
        • 2015-02-20
        • 2021-04-19
        • 1970-01-01
        • 2012-11-14
        • 2015-11-19
        • 1970-01-01
        相关资源
        最近更新 更多