3

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?

Mohammed Nagoor
  • 884
  • 2
  • 12
  • 25
srinu
  • 39
  • 5

4 Answers4

2

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

?>
Sasi varna kumar
  • 193
  • 1
  • 2
  • 11
1

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);
}
Sasi varna kumar
  • 193
  • 1
  • 2
  • 11
0

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.

Irfan
  • 7,029
  • 3
  • 42
  • 57
  • Hello Irfan,I am using joomla version 3.0.In that components no need to write save funtion.so i don't have save function in my component. – srinu Dec 25 '12 at 07:48
0

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.

Søren Beck Jensen
  • 1,676
  • 1
  • 12
  • 22