3
votes

I'm working on joomla component (com_book) version 3.0. In that I'm trying to insert books using form to database, in database I'm having updated_at column for that column I'm using timestamp datatype. Here updated_at column working fine when i'm inserting and updating books using database and inserting books using forms then it's working fine but when I'm trying updating books using forms that time update_at column not updating.

Can anyone explain what mistake I made in that process?

4
please provide your update statement? - Toretto
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP; This statement i am using for that updated_at coloumn. - srinu
Can you place your query in the question so we can figure it out - Toretto

4 Answers

2
votes

Another method is creating a custom field for that,

use this field code in your book.xml form file

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

and create a file called, lastmodified.php in your /administrator/components/com_book/models/fields folder

<?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;
    }
}

?>
1
votes

Paste this function in the Table file, of your table for example Joomla-Root/Administrator/com_book/table/book.php if there is already a store function, then edit that and add these lines alone man, that will do the job,

    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);
}
0
votes

In save function after data binding you can set the 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');

let me know if this not work.

0
votes

The Joomla Component Creator can build Joomla 3.0 components and save you messing with simple stuff like this.

Take a look: http://www.notwebdesign.com/joomla-component-creator/

It's free for one table. If nothing else you can copy paste code from that or peak and see how to develop yourself.