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

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.

  • Excellent Approach. Much appreciated. – Sahil Purav Apr 23 '15 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;
    }
  • JError is deprecated. See here: joomla.stackexchange.com/questions/223/… – Lodder Apr 22 '15 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 Apr 22 '15 at 8:33
  • I do it always :) – Sahil Purav Apr 22 '15 at 8:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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