Forums

Save Chronoforms V5 data in Chronoforms v4 table

angelmorales 12 Jan, 2016
Hello, I have a table that stores data from different Chronoforms V4 , now i discover a new funcion with Chronoforms V5 , so i create a new form with Chronoforms V5, but i need this data get stored in the Chronoforms V4 table, i have made a test, but the next colums arent get updated:
cf_id
cf_uid
cf_created
cf_modified
cf_created_by
cf_modified_by
cf_ipadress
cf_user_id

I dont want to change the columns name to fit chronoforms V5 (id,uniq_id,user_id, etc), what i need to change in V5 to accomplish this!?
I have trouble with the next stuff, i use to do it without problem with V4 but it doesnt works with V5, please help:
How does update and insert works in DB Save option?
How can I save the submit data in two different tables?

Thanks for your help!
GreyHead 13 Jan, 2016
Hi angelmorales,

Apart from the Primary Key column - cf_id here - which of these columns do you actually need saved? Have you ever used any of them?

There are a couple of solutions.

The simplest is to rename the columns to those that CFv5 uses by default

cf_uid to uniq_id
cf_created to created
cf_modified to modified
cf_user_id to user_id

If, for some reason, you need to keep the existing names then you can use a Custom code action before the DB Save action to set the values
<?php
$form->data['cf_uid'] = \GCore\Libs\Str::rand();
$form->data['cf_created'] = date('Y-m-d - h:i:s');
$juser = \JFactory::getUser();
$form->data['cf_user_id'] = $juser->id;
$form->data['created_by'] = $juser->id;
$form->data['cf_ipadress'] =  $_SERVER['REMOTE_ADDR'];
?>


CFv5 handled updates and inserts in exactly the same way as CFv4, if the primary key value is set and exists then the record is updated, otherwise a new record is created.

You can save in two tables using two DB Save actions set Model IDs if you need to separate out ambiguous data.

Bob

[[>> later : fixed typo <<]]
angelmorales 13 Jan, 2016
When the primary key value is set and exists the row is updated in the data base, I need to save the columns, :
cf_modified,cf_modified_by
When a new record is created, then a new row in the data base its created :
cf_id,cf_uid,cf_created,cf_created_by,cf_ipadress,cf_user_id, like chronoforms4 works

I use the Custome code but it appears an error , and always a new record its created , even if this record already exists

Thanks
angelmorales 14 Jan, 2016
I send you the images :
Chronoforms4 table columns, where i want to save the data,primary key cf_id

Chronoforms5 table columns, i think thats its how Chronoforms5 its trying to save the data.

So how can i save the data captured in Chronoforms5 in the Chronoforms4 table, example:
If the data exists "53000" in the cf_id column , i expect that the information in the row its going to be updated
If the data "53000" its not saved in the cf_id column , i expect that a new row its created in the database

Thanks!!
Max_admin 16 Jan, 2016
At the last code line there are a couple of equal signs "=", but only one should be there.

Regards,
Max
Max
ChronoForms developer...
Did you try ChronoMyAdmin for managing your Joomla database tables ?
angelmorales 18 Jan, 2016
It works , but i have a problem, when a record its searched ,if the primary key value is set and exists then the record is updated but in the database the columns "cf_created" and "cf_created_by" are updated and that its wrong , the correct action must be that the columns "cf_modifed" and "cf_modified_by" were updated

Thanks
GreyHead 19 Jan, 2016
Hi angelmorales,

You'd need to edit the code to check if this was an edit or a new record and set the parameters and values accordingly.

Bob
angelmorales 19 Jan, 2016
Sorry but i have not experience in php, I try to put two custome codes , one for update , and other for create a new record, each one with the values and parameters, but it doesnt work , could you help me with the correct code that can make the difference between updating and creating.
Thanks for your time and patience
angelmorales 21 Jan, 2016
I made it !!!, the data its saved in the correct column in the table, but I have an issue, I want to save the data in two tables, in the form setup-submit I put two DB SAVE for each table in the data base, when I submit the data only the first table in the DB SAVE after the Custome Code that you give me get saved, but the last DB SAVE doesnt,i try to exchange the DB SAVE but only one Table its updated.
If i put only one DB SAVE and submit the form, the table its updated with any problem.
Thanks for your help!
GreyHead 22 Jan, 2016
Hi angelmorales,

Well done.

Please drag a Debugger action into the On Submit event, then submit the form and post the debug results here. That should show the MySQL queries that are being created and let us know exactly what is happening.

Why do you need to save the same data to two tables - it is generally good design to avoid duplication?

Bob
angelmorales 22 Jan, 2016
I need to save the data in two tables because in the table "general33" its saved the recent data (It have a primary key, so the data gets updated)and in the table general33h its saved the record history , thats how I can see any change in the records (It doesnt have a primary key so there is always saved new records)

Thanks!
GreyHead 23 Jan, 2016
Hi angelmorales,

The ChronoForms and Joomla! DB Save code require the table to have an auto-incremented primary key - if there isn't one then they won't save. You can either add a primary key, or use Custom Code to create the MySQL query to update the second table.

Bob
angelmorales 25 Jan, 2016
With Chronoforms V4 the data its saved in the table without a primary key ,thats how new records are created instead updated, I think Chronoforms V5 could work the same.
I want to insert a custome code before the DB SAVE but i dont know how to write it, i know it must have the INSERT sentence but i dont know how to write for my purpose , coul you help me with an example???

Thanks!!
GreyHead 26 Jan, 2016
Hi angelmorales,

As far as I know the CFv4 DB Save action also requires a primary key. What is the problem with adding a primary key column to your second table?

Bob
angelmorales 26 Jan, 2016
The problemas its that if I add a primary key to the second table it would update the data and I need that the record always be a new récord, thats how I have been working for the las two years and it works fine , remember that this table is used by another chronoforms v4 , I will make a try adding a primary key and I will let you know if it works with the two forms with different versiones , (v4,v5)
Thanks
GreyHead 26 Jan, 2016
Hi angelmorales,

No, it will create a new record UNLESS the data you are saving contains an existing primary key value

Bob
angelmorales 26 Jan, 2016
It has, i am going to explain that, the form saves inventory data from stores of my bussiness, so all the numbers of the stores (ID_Tienda) already exists in this table and each store have its cf_id number (primary key), today I work with 3 chronoforms v4 and the data its saved in a table with a primary key that updates the data and in another table without a primary key that always creates a new record.(Image1), thats how I know any change in the inventory in each store.
In the Image2, I show the way that its configured the Event tab in chronoforms v4 , it have two DB Save and I have to tell you: two years working really fine!
Now I want to do the same thing with a form with ChronoformV5, I put two DB Save in the Setup tab (Image3) , but always it only saves in one table.
Sorry If I am so annoying, but thats the way that works form me, I am frustraded and confused because I expect that V5 do at least V4 use to do, so, I will very thankful if you can help me with a code to force Chronoforms V5 to save the data in my history table or any other way.
Another Example:In the Image1 you can see that the last record was created (cf_created) in 2015-01-28, a new capture was made with a ChronoformV4 and a new record was created in 2015-09-22, thats what I expect ChronoformV5 do.(Image4)
You must think : "Then keep using ChronoformsV4", but ChronoformsV5 has new features that I want to use from now .
Thanks for your help
GreyHead 30 Jan, 2016
Hi angelmorales,

ChronoForms v5 isn't exactly the same as CFv4 - in particular - for reasons that I have no idea about - Max renamed some of the default database column names and, at the same time, removed some of the code that added those values in the DB Save.

As you have seen, you can add that data using Custom Code if you need it.

As far as I know, nothing changed in the way the data is saved. Both versions use the Joomla! code which relies on a numeric, auto-incrementing primary key.

If you don't have a primary key then the DB Save wont update existing records. You can get round this by hand-coding the query in a Custom Code action.

Bob
angelmorales 03 Feb, 2016

You can get round this by hand-coding the query in a Custom Code action.


Could you help me with an example , I dont have experience with hand-coding query
Thank you very much
GreyHead 04 Feb, 2016
Hi angelmorales,

Something like this
<?php
$db = \JFactory::getDBO();
$query = "
    UPDATE `#__table_name`
      SET `column_name` = {$form->data['input_name']}, . . . // repeat as required // 
        WHERE `key_column_name` = '{$form->data['key_name']}' ;
";
$db->setQuery($query);
$db->execute();
?>

Bob
This topic is locked and no more replies can be posted.