Welcome, Guest
Username: Password: Remember me

TOPIC: [FIXED] save datas into two differents tables

[FIXED] save datas into two differents tables 26 Nov 2018 17:02 #15626

  • Nicolas
  • Nicolas's Avatar
  • Offline
  • Premium Member
  • Posts: 83
  • Thank you received: 12
  • Karma: 6
Hi there

I created a comment system on my "Tickets" view. All comments are displayed correctly and I have a textarea underneath to add a comment.

The model is "ticket" and I would like the text area to register in "Comments" (i have a comment model too).
I created a function in my forked ticket model but I can not separate the ticket ID from the comment ID. so when I comment on ticket id=2, comment id=2 is changed ...

on my forked ticket model :
public function getTable($type = 'commentaire', $prefix = 'Maintenance2Table', $config = array())
	{
		return JTable::getInstance($type, $prefix, $config);
	}

	public function getSaveComment($data){ 
	/**user_id**/
	$user =& JFactory::getUser();
	$userId = $user->get( 'id' );
	/**Date**/
	$date = JFactory::getDate();
	
	
    $data = new stdClass();
	$data->id = null;
	$data->created_by = $userId;
	$data->ticket = $this->item->ticket;
	$data->comment = null;
	$data->creation_date = $date;
    $db->insertObject( '#__maintenance2_commentaires', $data);  
	}

maybe I'm thinking the wrong way
Last Edit: 10 Feb 2019 08:19 by Nicolas.
The administrator has disabled public write access.

save datas in two differents tables 01 Feb 2019 18:12 #15638

  • Nicolas
  • Nicolas's Avatar
  • Offline
  • Premium Member
  • Posts: 83
  • Thank you received: 12
  • Karma: 6
Hello everyone !

I didn't find any answer to my question, though I've been looking for a while ...
I have in my forked model "ticket":
public function getTable($type = 'commentaire', $prefix = 'Maintenance2Table', $config = array())
	{
		return JTable::getInstance($type, $prefix, $config);
	}

	public function getSaveComment($data){ 
	/**user_id**/
	$user =& JFactory::getUser();
	$userId = $user->get( 'id' );
	/**Date**/
	$date = JFactory::getDate();
	
	
    $data = new stdClass();
	$data->id = null;
	$data->created_by = $userId;
	$data->ticket = $this->item->ticket;
	$data->comment = null;
	$data->creation_date = $date;
    $db->insertObject( '#__maintenance2_commentaires', $data);  
	}

I try to save a new line in the "comments" table like :
Id (key), ticketId, created_by, comment, creation_date.

The comment and the user register well when I click on "save", but I do not have a date and the "comment ID" is the "ticket ID" and the "ticketId" is "0" ...

I think the problem is that I try it from the model "ticket" and not by the model "comments"

Can someone help me ?
The administrator has disabled public write access.

save datas in two differents tables 06 Feb 2019 18:29 #15640

  • Romkabouter
  • Romkabouter's Avatar
  • Offline
  • Elite Member
  • Posts: 310
  • Thank you received: 131
  • Karma: 48
Hi Nicolas,

For this you do not need to fork the model, but fork the controller.
I assume you have a FK to the ticket in your comment table. (A comment is linked to a ticket right?)
In the forked controller you should add the function save, you can copy the whole function from the original controller.
public function save($key = null, $urlVar = null)

In that function, look for a codeblock like:
			$result = parent::save();
			//Get the model through postSaveHook()
			if ($this->model)
			{
				$model = $this->model;
				$item = $model->getItem();	
			}

Your comment save part should come after:
$item = $model->getItem();
and will probably look something like:
$commentModel = CkjModel::getInstance('comment','YourComponentModel');
$commentModel->save(array(
    'ticketid' => $item->id,
    'comment' => $item->comment
));					

good luck!
The administrator has disabled public write access.
The following user(s) said Thank You: Nicolas

save datas in two differents tables 09 Feb 2019 11:00 #15643

  • Nicolas
  • Nicolas's Avatar
  • Offline
  • Premium Member
  • Posts: 83
  • Thank you received: 12
  • Karma: 6
Hi Romkabouter

I tried in the controller as you suggested, but I can not recover the data of my input "update.form1-> commentaire"
for the moment I did this in my forked controller :
class Maintenance2ControllerTicket extends Maintenance2CkControllerTicket
{
	public function save($key = null, $urlVar = null)
	{	
		JSession::checkToken() or JSession::checkToken('get') or jexit(JText::_('JINVALID_TOKEN'));
		$result = parent::save();
		//Get the model through postSaveHook()
		if ($this->model)
		{
			$model = $this->model;
			$item = $model->getItem();
			$user = JFactory::getUser();
			$jinput = JFactory::getApplication()->input;

			$id = 0;
			$create_by = (int)$user->id;
			$ticketId = $item->id;
			$commentaire = $jinput->post->get('commentaire', '', 'RAW');;
			$creation_date = null;

			if (!empty($commentaire))
			{
				$commentaireModel = CkjModel::getInstance('Commentaire','Maintenance2CkModel');
				$commentaireModel->save(array(
					'id' => $id,
					'created_by' => $create_by,
					'ticket' => $ticketId,
					'comment' => $commentaire,
					'creation_date' => $date
				));
			}
		}
	}
}

but this one returns to me "null" in the "commentaire" input (all other things are corrects).
I had to change "comment" to "commentaire" (in french) because it duplicated comments instead of saving the new one...

maybe an idea ?

Thank in advance
The administrator has disabled public write access.

save datas in two differents tables 09 Feb 2019 11:52 #15644

  • Romkabouter
  • Romkabouter's Avatar
  • Offline
  • Elite Member
  • Posts: 310
  • Thank you received: 131
  • Karma: 48
I was assuming the comment was part of the ticket
But if you need the form values, you can use:

$form = $jinput->get('jform',array(),'ARRAY');
$commentaire =$form[commentaire'];
Last Edit: 09 Feb 2019 11:53 by Romkabouter.
The administrator has disabled public write access.

save datas in two differents tables 09 Feb 2019 12:37 #15645

  • Nicolas
  • Nicolas's Avatar
  • Offline
  • Premium Member
  • Posts: 83
  • Thank you received: 12
  • Karma: 6
I have two tables:
a ticket table with an FK comments to retrieve the comments based on ticketId and a comment table with an FK id ticket to insert the ticket ID at the same time as the comment.

So I added a comment form part so that it is added to the comments table with the ticket ID in my forked ticket.xml :
<fieldset name="update.form_1"
			addfieldpath="/administrator/components/com_maintenance2/models/fields"
			label="MAINTENANCE2_JFORM_AJOUTER_UN_COMMENTAIRE">

		<field name="commentaire"
				alias="commentaire"
				label="MAINTENANCE2_FIELD_COMMENTAIRE"
				type="cktextarea"/>

	</fieldset>

I just tried to get the form as you showed me but I still have a 'comment' = null;
do i have to declare the "$commentaire" on the model too?

Thx
The administrator has disabled public write access.

save datas in two differents tables 09 Feb 2019 13:39 #15646

  • Romkabouter
  • Romkabouter's Avatar
  • Offline
  • Elite Member
  • Posts: 310
  • Thank you received: 131
  • Karma: 48
commentaire should be a field in your table/form. If it is called differtent, use that name.

You can do a var_dump($form) to see of the form has values and what they are.
Do a var_dump() and exit;
The administrator has disabled public write access.

save datas in two differents tables 09 Feb 2019 17:02 #15647

  • Nicolas
  • Nicolas's Avatar
  • Offline
  • Premium Member
  • Posts: 83
  • Thank you received: 12
  • Karma: 6
i alerady tested with a var_dump($commentaire)
i tried with a var_dump($form); and it returns :
C:\wamp\www\components\com_maintenance2\fork\controllers\ticket.php:52:
array (size=2)
  'commentaire' => string 'test for Romkabouter
with var_dump($form);' (length=43)
  'solution' => string '<p>test</p>' (length=11)
Is there a chance for it that also try to register on the ticket table?

edit :
In the fork of the model ticket I tried to call the table "comments" before save like this:
public function getTable($type = 'commentaire', $prefix = 'Maintenance2Table', $config = array())
	{
		return JTable::getInstance($type, $prefix, $config);
	}

the problem was that the "solution" was also trying to register in the comment table ...

I have to try to separate the datas to send each in differents tables
Last Edit: 09 Feb 2019 17:12 by Nicolas. Reason: complement
The administrator has disabled public write access.

save datas in two differents tables 09 Feb 2019 22:45 #15648

  • Romkabouter
  • Romkabouter's Avatar
  • Offline
  • Elite Member
  • Posts: 310
  • Thank you received: 131
  • Karma: 48
Your first var_dump log shows that the form is correctly cathed, you see that the values was "'test for Romkabouter"
This leads me to think that after $commentaire =$form["commentaire"];
$commentaire will hold the string 'test for Romkabouter"

So, instead of
$commentaire = $jinput->post->get('commentaire', '', 'RAW');;
do
$commentaire =$form["commentaire"];

The complate function save will be this (I have left out create date because you use $date which was not declared, I also simplified a bit):
	public function save($key = null, $urlVar = null)
	{	
		JSession::checkToken() or JSession::checkToken('get') or jexit(JText::_('JINVALID_TOKEN'));
		$result = parent::save();
		//Get the model through postSaveHook()
		if ($this->model)
		{
			$model = $this->model;
			$item = $model->getItem();
			$user = JFactory::getUser();
			$jinput = JFactory::getApplication()->input;
                        $form = $jinput->get('jform',array(),'ARRAY');
                        $commentaire =$form["commentaire"];

			if (!empty($commentaire))
			{
				$commentaireModel = CkjModel::getInstance('Commentaire','Maintenance2CkModel');
				$commentaireModel->save(array(
					'id' => 0,
					'created_by' => (int)$user->id,
					'ticket' =>  $item->id,
					'comment' => $commentaire
				));
			}
		}
	}

I am using an almost similar in a forked controller as will, which works fine.
Remove the fork of the table, you do not need it.

EDIT: I think I read it wrong, you did a var_dump twice.... but $commentaire shows a correct value right?
If is does not work, I'd first try to hard code some values. Just to see of you can save data into the comments table
Last Edit: 09 Feb 2019 22:53 by Romkabouter.
The administrator has disabled public write access.
The following user(s) said Thank You: Nicolas

save datas in two differents tables 10 Feb 2019 00:50 #15649

  • Nicolas
  • Nicolas's Avatar
  • Offline
  • Premium Member
  • Posts: 83
  • Thank you received: 12
  • Karma: 6
Well here is the problem.
I could call
$commentaire =$form["commentaire"];
But not
$commentaire = $jinput->post->get('commentaire', '', 'RAW');
i had to go through the form to find my input ...

Now I will look how to change the messages after the backup =)

Thank you ROMKABOUTER for your precious help.

Nicolas
The administrator has disabled public write access.

save datas in two differents tables 10 Feb 2019 07:25 #15650

  • Romkabouter
  • Romkabouter's Avatar
  • Offline
  • Elite Member
  • Posts: 310
  • Thank you received: 131
  • Karma: 48
Also, I you want to save a comment with a new commentaire, you can us id => comment->id

model->save will then update the record with the specified id

Good luck
The administrator has disabled public write access.
The following user(s) said Thank You: Nicolas
Time to create page: 0.082 seconds

Get Started