1

I'm using JTable for CRUD on my application. I've created method in model to save data in database

I need to add custom inputs "created" and "created_by" as shown in "$custom_data" variable and save it in database. Please note that, $form_data is getting saved successfully.

public function create() {
        $table = $this->getTable();
        $jinput = JFactory::getApplication()->input->post;
        $form_data = $jinput->get('jform', array(), 'array');
        $custom_data = array(
            'created'       => JFactory::getDate()->toSql(),
            'created_by'    => JFactory::getUser()->id
        );

        if (!$table->save($form_data)) {
            JError::raiseNotice(500, $table->getError());
            return FALSE;
        }

        return $form_data;
    }

2 Answers 2

2

You asked for another method, here it is :-)

The best place for such operations IMHO is the bind method of the JTable class. Below is an example how to override this method in your JTable class.

public function bind($src, $ignore = array())
{
    // Call parent's bind method
    if (parent::bind($src, $ignore))
    {
        // Do the following only when it's a new item ($this->id is 0 then)
        if (!$this->id)
        {
            $this->created = JFactory::getDate()->toSql();
            $this->created_by = JFactory::getUser()->id;
        }
    }
    return true;
}

You don't need these lines anymore then:

$custom_data = array(
    'created' => JFactory::getDate()->toSql(),
    'created_by' => JFactory::getUser()->id
);
$data += $custom_data;

You are calling $table->save() in your model and this method will automatically call the bind() method for you.

1
  • Excellent Approach. Much appreciated. Commented Apr 23, 2015 at 5:35
1

I was able to solve this issue with following:

just added $data = $form_data + $custom_data; Any other recommended method is appreciable.

public function create() {
        $table = $this->getTable();
        $jinput = JFactory::getApplication()->input->post;
        $data = $jinput->get('jform', array(), 'array');
        $custom_data = array(
            'created' => JFactory::getDate()->toSql(),
            'created_by' => JFactory::getUser()->id
        );
        $data += $custom_data;
        try {
            $table->save($data);
        } catch (Exception $ex) {
            if ($ex->getCode() == '404') {
                throw new Exception($e->getMessage(), 404);
            } else {
                JFactory::getApplication()->enqueueMessage(JText::_('COM_HELPDESK_ERROR_OCCURRED'), 'error');
            }
        }

        $id = array(
            'id' => $table->id
        );

        $data += $id;

        return $data;
    }
3
  • JError is deprecated. See here: joomla.stackexchange.com/questions/223/…
    – Lodder
    Commented Apr 22, 2015 at 7:01
  • Good good :) Glad you solved it in the end. Don't forget to accept your answer when you're allowed to, so people can see it has been solved
    – Lodder
    Commented Apr 22, 2015 at 8:33
  • I do it always :) Commented Apr 22, 2015 at 8:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.