2

I'm creating custom helpdesk ticketing component, where I need to track user activities. For e.g., If user have created the ticket, then it should get save to history table. Let's say user wants to update one of the field (suppose it is subject of ticket), he can just edit subject in the form and clicking on submit button will fire JTable->save($data) method which will result in saving of corresponding field (in this case it is Subject)

My question is, How do I know which field(s) are getting updated in database?

I will get those fields and save in a database like this:

John Doe has modified Subject of Ticket on 05/19/2015

John Doe has Created Ticket on 05/19/2015

Do I need to write a plugin to achieve this? I don't expect to have a code. Just a small kick-start would help me.

  • When firing save($data) can you write in a separate table all actions like: user - action - subject - time? You just need to modify your component properly. – pl71 May 23 '15 at 20:47
1

Your question is not entirely correct. Your submit button is invoking a method save() on a controller. You can find out the correct controller by inspecting the task field of the form

<input name="task" value="controllername.save" />

or it could be in a parameter of the form's action attribute i.e.

<form action="index.php?option=com_component&task=controllername.save">

Now open the controllername.php, and find the save() method.

There you will find a model being instantiated; in case it's implicit, such as $this->getModel() look for a model named like the controller. Otherwise the name will be spelled out.

Open the model, and find the save() function there. Most likely it will get an instance of a JTable object, then invoke the save() method on it.

Now at the very start of the save() function in your model, you will have the new data available, and not yet persisted to the database.

If it's an edit, the id field will be set, else in case of a new record it will be 0 or null.

If the id is valid, query the database for the current values, i.e.

$oldValues = $db->setQuery('select * from #__tablename where id='.$db->quote($id))->loadObject();

The look down at the function and find the place where $someTableInstance.save() is invoked, and examine the result. If it succeeds, then the edit was successful.

You can now check the previous data you stored in $oldValues, compare it with the content of $data, and create your desired history / logging features.

  • Well I was really short at asking an question. But surely I'm doing the exact way as you mentioned. That should help me to go further. Thanks for your answer – Sahil Purav May 27 '15 at 5:13

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.