How to have only 1 record per user in DB

AloneAngel 12 Apr, 2015
Hello,

I'm trying to make additional profile fields for the users using Chronoforms V5.
Everything looks good for the moment but the problem is that after each submit I got new record in DB.
I've created table for my form using Chronoforms "Create Table" functionality without changing any parameters and remaining them with default values.
Force Save is disabled and I'm still getting this behavior.

Could you suggest me, please, what I can do to have only 1 record per user in DB?
GreyHead 13 Apr, 2015
Hi AloneAngel,

To use the automatic INSERT/UPDATE switch for records you need to include the record id of the record to be updated in the form data. If this exists in the table that record will be updated; if ti doesn't exist, or is blank, then a new record is created.

Bob
AloneAngel 13 Apr, 2015
Hello, Bob,

Thank you for your answer.
I thought that this is a default behavior and I'm doing something wrong.
According to your comment, am I right that I need first to retrieve corresponding item from DB and then replace it in the resulting array?
I got another idea (not very neat from the DB side): to set id equals to user_id. In that case I will have each record only once per user but from DB view it won't be very good as far as I will have ids that are not consequent.

What do you think about this?

Dmytro
GreyHead 13 Apr, 2015
Answer
Hi Dmytro,
Using the User ID will work provided that all your form users are logged in*. BUT it's more complicated if some records already exist and others are new. You'd need to write custom MySQL.

Much simpler to check if there is already a record for the user and then add the record id to the $form->data array. Something like this:
<?php
$user = JFactory::getUser();
$db =& JFactory::getDBO();
$query = "
    SELECT `id`
        FROM `#__some_table`
        WHERE `user_id` = '{$user->id}' ;
";
$db->setQuery($query);
$id = $db->loadResult();
If ( $id != '' ) {
  $form->data['id'] = $id;
}
?>

Bob
AloneAngel 13 Apr, 2015
Thank you very much, Bob.

I will try this. Looks really good)
AloneAngel 14 Apr, 2015
Finally, I've finished without doing request to DB (to lower number of requests to DB) and having just this code inside DBRead and DBSave Conditions:
<?php 
$user_id = JFactory::getUser()->id;
if ($user_id != 0) {
      return array("user_id" => $user_id);
}
?>


Is this ok, Bob?

Dmytro
Max_admin 14 Apr, 2015
Hi Dmytro,

You must read the existing record info from the db as Bob has posted in order to retrieve the primary key value of the an existing record.

Regards,
Max
Max, ChronoForms developer
ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
AloneAngel 14 Apr, 2015
Hello, Max,

Thank you for your response.
But why do I need to have id? Isn't it possible to make conditions on user_id?
Max_admin 14 Apr, 2015
You are checking if there is an existing record with that user id, if there is then you pass the primary key value to update it, but if not then you pass nothing to get a new record inserted.
Max, ChronoForms developer
ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
AloneAngel 14 Apr, 2015
Hello, Max,

There's Conditions part in DBSave action which allows you to update items which meet condition or to insert new one if none of the items were found according to the specified condition.
Am I understanding something wrong?

Thank you for your help.

Dmytro
Max_admin 14 Apr, 2015
Hi Dmytro,

According to the field help:
if this field is not empty then there will be always an UPDATE but never an INSERT.

So if you use that field then it will always try to execute an update, you can use this if you already have a record for each user, but of not then you have to test if there is a record first as Bob suggested.

Another way to do it is using a combination of a "DB Read" + 2 x "DB Save", the "DB Read" will check if there is a record for the current user, if there is then you will have a "DB Save" with update condition in the "On found" event of the db read, if not then you should have the other "DB Save" in the "on not found" event of the "db read", that one will do an insert.

Regards,
Max
Max, ChronoForms developer
ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
AloneAngel 14 Apr, 2015
Now it' clear.
Thank you very much, Max.
GreyHead 15 Apr, 2015
Hi AloneAngel,

The Help text for the Update Conditions box says "if this field is not empty then there will be always an UPDATE but never an INSERT." So, if you put anything in there you will never get a new record created.

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